-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
PushToIntegrationTrait.php
94 lines (83 loc) · 2.99 KB
/
PushToIntegrationTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
namespace Mautic\PluginBundle\EventListener;
use Mautic\LeadBundle\Entity\Lead;
use Mautic\PluginBundle\Helper\IntegrationHelper;
use Mautic\PluginBundle\Integration\AbstractIntegration;
/**
* Static methods must be used due to the Point triggers not being converted to Events yet
* Once that happens, this can be converted to a standard method classes.
*
* Trait PushToIntegrationTrait
*/
trait PushToIntegrationTrait
{
/**
* @var IntegrationHelper
*/
protected static $integrationHelper;
/**
* Used by methodCalls to event subscribers.
*/
public function setIntegrationHelper(IntegrationHelper $integrationHelper)
{
static::setStaticIntegrationHelper($integrationHelper);
}
/**
* Used by callback methods such as point triggers.
*/
public static function setStaticIntegrationHelper(IntegrationHelper $integrationHelper)
{
static::$integrationHelper = $integrationHelper;
}
/**
* @param $lead
*/
protected function pushToIntegration(array $config, Lead $lead, array &$errors = [])
{
return static::pushIt($config, $lead, $errors);
}
/**
* Used because the Point trigger actions have not been converted to Events yet and thus must leverage a callback.
*
* @param array<string, array<string, int|string>|null> $config
* @param Lead $lead
* @param mixed[] $errors
*
* @return bool
*/
protected static function pushIt($config, $lead, &$errors)
{
$integration = (!empty($config['integration'])) ? $config['integration'] : null;
$integrationCampaign = (!empty($config['config']['campaigns'])) ? $config['config']['campaigns'] : null;
$services = static::$integrationHelper->getIntegrationObjects($integration);
$success = true;
/**
* @var string
* @var AbstractIntegration $s
*/
foreach ($services as $s) {
$settings = $s->getIntegrationSettings();
if (!$settings->isPublished()) {
continue;
}
$personIds = null;
if (method_exists($s, 'pushLead')) {
if (!$personIds = $s->resetLastIntegrationError()->pushLead($lead, $config)) {
$success = false;
if ($error = $s->getLastIntegrationError()) {
$errors[] = $error;
}
}
}
if ($success && $integrationCampaign && method_exists($s, 'pushLeadToCampaign')) {
if (!$s->resetLastIntegrationError()->pushLeadToCampaign($lead, $integrationCampaign, $personIds, $config)) {
$success = false;
if ($error = $s->getLastIntegrationError()) {
$errors[] = $error;
}
}
}
}
return $success;
}
}