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

SyncTo   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 0
loc 52
ccs 0
cts 29
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getTriggerLabel() 0 4 1
A getConfirmationMessage() 0 7 1
A performAction() 0 25 4
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