Completed
Push — master ( 136153...d5a3eb )
by Nate
19:29 queued 17:38
created

SyncTo   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
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 48
ccs 0
cts 26
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 21 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($element, $field)) {
53
            $this->setMessage("Failed to sync from HubSpot Object");
54
            return false;
55
        }
56
57
        $element->setFieldValue($field->handle, null);
58
59
        $this->id = $query->select(['objectId'])->scalar();
60
61
        $this->setMessage("Sync to HubSpot executed successfully");
62
        return true;
63
    }
64
}
65