|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
|
5
|
|
|
* @license https://flipboxfactory.com/software/force/license |
|
6
|
|
|
* @link https://www.flipboxfactory.com/software/force/ |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace flipbox\craft\salesforce\queue; |
|
10
|
|
|
|
|
11
|
|
|
use Craft; |
|
12
|
|
|
use craft\base\Element; |
|
13
|
|
|
use craft\base\ElementInterface; |
|
14
|
|
|
use craft\helpers\ArrayHelper; |
|
15
|
|
|
use flipbox\craft\ember\helpers\SiteHelper; |
|
16
|
|
|
use flipbox\craft\integration\queries\IntegrationAssociationQuery; |
|
17
|
|
|
use flipbox\craft\integration\records\IntegrationAssociation; |
|
18
|
|
|
use flipbox\craft\salesforce\fields\Objects; |
|
19
|
|
|
use flipbox\craft\salesforce\transformers\PopulateElementErrorsFromResponse; |
|
20
|
|
|
use flipbox\craft\salesforce\transformers\PopulateElementFromResponse; |
|
21
|
|
|
use Flipbox\Salesforce\Resources\SObject; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Sync a Salesforce Object to a Craft Element |
|
25
|
|
|
*/ |
|
26
|
|
|
class SyncElementFromSalesforceObjectJob extends AbstractSyncElementJob |
|
27
|
|
|
{ |
|
28
|
|
|
use ResolveObjectIdFromElementTrait; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string|null |
|
32
|
|
|
*/ |
|
33
|
|
|
public $objectId; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
public $transformer = [ |
|
39
|
|
|
'class' => PopulateElementFromResponse::class, |
|
40
|
|
|
'action' => 'sync' |
|
41
|
|
|
]; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param \craft\queue\QueueInterface|\yii\queue\Queue $queue |
|
45
|
|
|
* @return bool |
|
46
|
|
|
* @throws \Throwable |
|
47
|
|
|
* @throws \craft\errors\ElementNotFoundException |
|
48
|
|
|
* @throws \flipbox\craft\ember\exceptions\RecordNotFoundException |
|
49
|
|
|
* @throws \yii\base\Exception |
|
50
|
|
|
* @throws \yii\base\InvalidConfigException |
|
51
|
|
|
*/ |
|
52
|
|
|
public function execute($queue) |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->syncDown( |
|
55
|
|
|
$this->getElement(), |
|
56
|
|
|
$this->getField(), |
|
57
|
|
|
$this->objectId |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param ElementInterface $element |
|
63
|
|
|
* @param Objects $field |
|
64
|
|
|
* @param string $objectId |
|
65
|
|
|
* @return bool |
|
66
|
|
|
* @throws \Throwable |
|
67
|
|
|
* @throws \craft\errors\ElementNotFoundException |
|
68
|
|
|
* @throws \flipbox\craft\ember\exceptions\RecordNotFoundException |
|
69
|
|
|
* @throws \yii\base\Exception |
|
70
|
|
|
* @throws \yii\base\InvalidConfigException |
|
71
|
|
|
*/ |
|
72
|
|
|
public function syncDown( |
|
73
|
|
|
ElementInterface $element, |
|
74
|
|
|
Objects $field, |
|
75
|
|
|
string $objectId = null |
|
76
|
|
|
): bool { |
|
77
|
|
|
|
|
78
|
|
|
$id = $objectId ?: $this->resolveObjectIdFromElement($element, $field); |
|
79
|
|
|
if (null === $id) { |
|
80
|
|
|
return false; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$response = SObject::read( |
|
84
|
|
|
$field->getConnection(), |
|
85
|
|
|
$field->getCache(), |
|
86
|
|
|
$field->object, |
|
87
|
|
|
$id |
|
88
|
|
|
); |
|
89
|
|
|
|
|
90
|
|
|
if (($response->getStatusCode() < 200 || $response->getStatusCode() > 300)) { |
|
91
|
|
|
call_user_func_array( |
|
92
|
|
|
new PopulateElementErrorsFromResponse(), |
|
93
|
|
|
[ |
|
94
|
|
|
$response, |
|
95
|
|
|
$element, |
|
96
|
|
|
$field, |
|
97
|
|
|
$id |
|
98
|
|
|
] |
|
99
|
|
|
); |
|
100
|
|
|
return false; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
if (null !== ($transformer = $this->resolveTransformer($this->transformer))) { |
|
104
|
|
|
call_user_func_array( |
|
105
|
|
|
$transformer, |
|
106
|
|
|
[ |
|
107
|
|
|
$response, |
|
108
|
|
|
$element, |
|
109
|
|
|
$field, |
|
110
|
|
|
$id |
|
111
|
|
|
] |
|
112
|
|
|
); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
if ($objectId !== null) { |
|
116
|
|
|
$this->addAssociation( |
|
117
|
|
|
$element, |
|
118
|
|
|
$field, |
|
119
|
|
|
$id |
|
120
|
|
|
); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return Craft::$app->getElements()->saveElement($element); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param ElementInterface|Element $element |
|
128
|
|
|
* @param Objects $field |
|
129
|
|
|
* @param string $id |
|
130
|
|
|
*/ |
|
131
|
|
|
protected function addAssociation( |
|
132
|
|
|
ElementInterface $element, |
|
133
|
|
|
Objects $field, |
|
134
|
|
|
string $id |
|
135
|
|
|
) { |
|
136
|
|
|
/** @var IntegrationAssociation $recordClass */ |
|
137
|
|
|
$recordClass = $field::recordClass(); |
|
138
|
|
|
|
|
139
|
|
|
/** @var IntegrationAssociationQuery $associations */ |
|
140
|
|
|
$associationQuery = $element->getFieldValue($field->handle); |
|
141
|
|
|
$associations = ArrayHelper::index($associationQuery->all(), 'objectId'); |
|
142
|
|
|
|
|
143
|
|
|
if (!array_key_exists($id, $associations)) { |
|
144
|
|
|
$association = new $recordClass([ |
|
145
|
|
|
'element' => $element, |
|
146
|
|
|
'field' => $field, |
|
147
|
|
|
'siteId' => SiteHelper::ensureSiteId($element->siteId), |
|
148
|
|
|
'objectId' => $id |
|
149
|
|
|
]); |
|
150
|
|
|
|
|
151
|
|
|
$associations = array_merge( |
|
152
|
|
|
$associationQuery->all(), |
|
153
|
|
|
[ |
|
154
|
|
|
$association |
|
155
|
|
|
] |
|
156
|
|
|
); |
|
157
|
|
|
|
|
158
|
|
|
$associationQuery->setCachedResult(array_values($associations)); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|