Completed
Push — develop ( 76527a...6d8075 )
by Nate
03:43
created

SyncTo   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 0
loc 51
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 24 3
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\hubspot\fields\actions;
10
11
use Craft;
12
use craft\base\ElementInterface;
13
use flipbox\hubspot\db\ObjectAssociationQuery;
14
use flipbox\hubspot\fields\Objects;
15
use yii\web\HttpException;
16
17
class SyncTo extends AbstractObjectAction
18
{
19
    /**
20
     * @inheritdoc
21
     */
22
    public function getTriggerLabel(): string
23
    {
24
        return Craft::t('hubspot', 'Create HubSpot Object from Element');
25
    }
26
27
    /**
28
     * @inheritdoc
29
     */
30
    public function getConfirmationMessage()
31
    {
32
        return Craft::t(
33
            'hubspot',
34
            "This element will be used to create a new HubSpot Object.  Please confirm to continue."
35
        );
36
    }
37
38
    /**
39
     * @inheritdoc
40
     * @throws HttpException
41
     * @throws \yii\base\InvalidConfigException
42
     */
43
    public function performAction(Objects $field, ElementInterface $element): bool
44
    {
45
        /** @var ObjectAssociationQuery $query */
46
        if (null === ($query = $element->getFieldValue($field->handle))) {
47
            throw new HttpException(400, 'Field is not associated to element');
48
        }
49
50
        $resource = $field->getResource();
51
52
        if (!$resource->syncUp(
53
            $element,
54
            $field
55
        )) {
56
            $this->setMessage("Failed to sync from HubSpot Object");
57
            return false;
58
        }
59
60
        $element->setFieldValue($field->handle, null);
61
62
        $this->id = $query->select(['objectId'])->scalar();
63
64
        $this->setMessage("Sync to HubSpot executed successfully");
65
        return true;
66
    }
67
}
68