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\SyncElementToHubSpotJob; |
16
|
|
|
use yii\base\Action; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Flipbox Factory <[email protected]> |
20
|
|
|
* @since 1.0.0 |
21
|
|
|
*/ |
22
|
|
|
abstract class AbstractSyncTo extends Action |
23
|
|
|
{ |
24
|
|
|
use CheckAccessTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param ElementInterface $element |
28
|
|
|
* @param Objects $field |
29
|
|
|
* @return ElementInterface|mixed |
30
|
|
|
* @throws \yii\web\UnauthorizedHttpException |
31
|
|
|
*/ |
32
|
|
|
protected function runInternal( |
33
|
|
|
ElementInterface $element, |
34
|
|
|
Objects $field |
35
|
|
|
) { |
36
|
|
|
// Check access |
37
|
|
|
if (($access = $this->checkAccess($element, $field)) !== true) { |
38
|
|
|
return $access; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if (false === $this->performAction($element, $field)) { |
42
|
|
|
return $this->handleFailResponse($element); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $this->handleSuccessResponse($element); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param ElementInterface $element |
50
|
|
|
* @param Objects $field |
51
|
|
|
* @return bool |
52
|
|
|
*/ |
53
|
|
|
protected function performAction( |
54
|
|
|
ElementInterface $element, |
55
|
|
|
Objects $field |
56
|
|
|
) { |
57
|
|
|
$job = new SyncElementToHubSpotJob([ |
58
|
|
|
'element' => $element, |
59
|
|
|
'field' => $field |
60
|
|
|
]); |
61
|
|
|
|
62
|
|
|
return $job->execute(Craft::$app->getQueue()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param ElementInterface $element |
67
|
|
|
* @return ElementInterface |
68
|
|
|
*/ |
69
|
|
|
protected function handleSuccessResponse(ElementInterface $element) |
70
|
|
|
{ |
71
|
|
|
// Success status code |
72
|
|
|
Craft::$app->getResponse()->setStatusCode($element ? 200 : 201); |
73
|
|
|
return $element; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param ElementInterface $element |
78
|
|
|
* @return ElementInterface |
79
|
|
|
*/ |
80
|
|
|
protected function handleFailResponse(ElementInterface $element) |
81
|
|
|
{ |
82
|
|
|
Craft::$app->getResponse()->setStatusCode(400); |
83
|
|
|
return $element; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|