1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://flipboxfactory.com/software/hubspot/license |
6
|
|
|
* @link https://www.flipboxfactory.com/software/hubspot/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\craft\hubspot\cp\actions\sync; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use craft\base\ElementInterface; |
13
|
|
|
use flipbox\craft\ember\actions\CheckAccessTrait; |
14
|
|
|
use flipbox\craft\hubspot\fields\Objects; |
15
|
|
|
use flipbox\craft\hubspot\queue\SyncElementFromHubSpotJob; |
16
|
|
|
use yii\base\Action; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Flipbox Factory <[email protected]> |
20
|
|
|
* @since 1.0.0 |
21
|
|
|
*/ |
22
|
|
|
abstract class AbstractSyncFrom extends Action |
23
|
|
|
{ |
24
|
|
|
use CheckAccessTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param ElementInterface $element |
28
|
|
|
* @param Objects $field |
29
|
|
|
* @param string|null $objectId |
30
|
|
|
* @return ElementInterface|mixed |
31
|
|
|
* @throws \yii\web\UnauthorizedHttpException |
32
|
|
|
*/ |
33
|
|
|
protected function runInternal( |
34
|
|
|
ElementInterface $element, |
35
|
|
|
Objects $field, |
36
|
|
|
string $objectId = null |
37
|
|
|
) { |
38
|
|
|
// Check access |
39
|
|
|
if (($access = $this->checkAccess($element, $field, $objectId)) !== true) { |
40
|
|
|
return $access; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if (false === $this->performAction($element, $field, $objectId)) { |
44
|
|
|
return $this->handleFailResponse($element); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $this->handleSuccessResponse($element); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param ElementInterface $element |
52
|
|
|
* @param Objects $field |
53
|
|
|
* @param string|null $objectId |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
|
|
protected function performAction( |
57
|
|
|
ElementInterface $element, |
58
|
|
|
Objects $field, |
59
|
|
|
string $objectId = null |
60
|
|
|
) { |
61
|
|
|
$job = new SyncElementFromHubSpotJob([ |
62
|
|
|
'element' => $element, |
63
|
|
|
'field' => $field, |
64
|
|
|
'objectId' => $objectId |
65
|
|
|
]); |
66
|
|
|
|
67
|
|
|
return $job->execute(Craft::$app->getQueue()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param ElementInterface $element |
72
|
|
|
* @return ElementInterface |
73
|
|
|
*/ |
74
|
|
|
protected function handleSuccessResponse(ElementInterface $element) |
75
|
|
|
{ |
76
|
|
|
Craft::$app->getResponse()->setStatusCode(200); |
77
|
|
|
return $element; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param ElementInterface $element |
82
|
|
|
* @return ElementInterface |
83
|
|
|
*/ |
84
|
|
|
protected function handleFailResponse(ElementInterface $element) |
85
|
|
|
{ |
86
|
|
|
Craft::$app->getResponse()->setStatusCode(400); |
87
|
|
|
return $element; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|