Completed
Push — develop ( 2d4f17...d5975b )
by Nate
05:25
created

SyncTo::performAction()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 0
cts 18
cp 0
rs 9.52
c 0
b 0
f 0
cc 4
nc 4
nop 2
crap 20
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\fields\actions;
10
11
use Craft;
12
use craft\base\ElementInterface;
13
use flipbox\craft\hubspot\fields\ObjectsFieldInterface;
14
use flipbox\craft\integration\fields\actions\AbstractIntegrationAction;
15
use flipbox\craft\integration\fields\Integrations;
16
use flipbox\craft\integration\queries\IntegrationAssociationQuery;
17
use yii\web\HttpException;
18
19
class SyncTo extends AbstractIntegrationAction
20
{
21
    /**
22
     * @inheritdoc
23
     */
24
    public function getTriggerLabel(): string
25
    {
26
        return Craft::t('hubspot', 'Create HubSpot Object from Element');
27
    }
28
29
    /**
30
     * @inheritdoc
31
     */
32
    public function getConfirmationMessage()
33
    {
34
        return Craft::t(
35
            'hubspot',
36
            "This element will be used to create a new HubSpot Object.  Please confirm to continue."
37
        );
38
    }
39
40
    /**
41
     * @inheritdoc
42
     * @throws HttpException
43
     * @throws \Throwable
44
     */
45
    public function performAction(Integrations $field, ElementInterface $element): bool
46
    {
47
        if (!$field instanceof ObjectsFieldInterface) {
48
            $this->setMessage("Invalid field type.");
49
            return false;
50
        }
51
52
        /** @var IntegrationAssociationQuery $query */
53
        if (null === ($query = $element->getFieldValue($field->handle))) {
54
            throw new HttpException(400, 'Field is not associated to element');
55
        }
56
57
        if (!$field->syncToHubSpot($element)) {
58
            $this->setMessage("Failed to sync from HubSpot Object");
59
            return false;
60
        }
61
62
        // Reset
63
        $element->setFieldValue($field->handle, null);
64
65
        $this->id = $query->select(['objectId'])->scalar();
66
67
        $this->setMessage("Sync to HubSpot executed successfully");
68
        return true;
69
    }
70
}
71