Completed
Push — master ( 99c3bc...8ae47d )
by Nate
11:33 queued 09:36
created

Associate::validateResource()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 0
cts 16
cp 0
rs 9.568
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
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\actions\objects;
10
11
use Craft;
12
use flipbox\hubspot\HubSpot;
13
use flipbox\hubspot\records\ObjectAssociation;
14
use flipbox\hubspot\transformers\collections\TransformerCollection;
15
use yii\base\DynamicModel;
16
use yii\base\Model;
17
18
/**
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 1.0.0
21
 */
22
class Associate extends AbstractAssociationAction
23
{
24
    /**
25
     * Validate that the HubSpot Object exists prior to associating
26
     *
27
     * @var bool
28
     */
29
    public $validate = true;
30
31
    /**
32
     * @param string $field
33
     * @param string $element
34
     * @param string $newObjectId
35
     * @param string|null $objectId
36
     * @param int|null $siteId
37
     * @param int|null $sortOrder
38
     * @return Model
39
     * @throws \flipbox\ember\exceptions\NotFoundException
40
     * @throws \yii\web\HttpException
41
     */
42
    public function run(
43
        string $field,
44
        string $element,
45
        string $newObjectId,
46
        string $objectId = null,
47
        int $siteId = null,
48
        int $sortOrder = null
49
    ) {
50
        // Resolve Field
51
        $field = $this->resolveField($field);
52
53
        // Resolve Element
54
        if (null === ($sourceElement = Craft::$app->getElements()->getElementById($element))) {
55
            return $this->handleInvalidElementResponse($element);
56
        }
57
58
        // Resolve Site Id
59
        if (null === $siteId) {
60
            $siteId = Craft::$app->getSites()->currentSite->id;
61
        }
62
63
        // Find existing?
64
        if (!empty($objectId)) {
65
            $association = HubSpot::getInstance()->getObjectAssociations()->getByCondition([
66
                'objectId' => $objectId,
67
                'elementId' => $sourceElement->getId(),
68
                'fieldId' => $field->id,
69
                'siteId' => $siteId,
70
            ]);
71
        } else {
72
            $association = HubSpot::getInstance()->getObjectAssociations()->create([
73
                'elementId' => $sourceElement->getId(),
74
                'fieldId' => $field->id,
75
                'siteId' => $siteId,
76
77
            ]);
78
        }
79
80
        $association->objectId = $newObjectId;
81
        $association->sortOrder = $sortOrder;
82
83
        return $this->runInternal($association);
84
    }
85
86
    /**
87
     * @inheritdoc
88
     * @param ObjectAssociation $model
89
     * @throws \flipbox\ember\exceptions\RecordNotFoundException
90
     * @throws \Exception
91
     */
92
    protected function performAction(Model $model): bool
93
    {
94
        if (true === $this->ensureAssociation($model)) {
95
            if ($this->validate === true && !$this->validateResource($model)) {
96
                return false;
97
            }
98
99
            return HubSpot::getInstance()->getObjectAssociations()->associate(
100
                $model
101
            );
102
        }
103
104
        return false;
105
    }
106
107
    /**
108
     * @param ObjectAssociation $record
109
     * @return bool
110
     * @throws \Exception
111
     */
112
    protected function validateResource(
113
        ObjectAssociation $record
114
    ): bool {
115
116
        if (null === ($fieldId = $record->fieldId)) {
117
            return false;
118
        }
119
120
        if (null === ($field = HubSpot::getInstance()->getObjectsField()->findById($fieldId))) {
121
            return false;
122
        }
123
124
        $criteria = $field->getResource()->getAccessorCriteria([
125
            'id' => $record->objectId,
126
            'transformer' => TransformerCollection::class
127
        ]);
128
129
        /** @var DynamicModel $response */
130
        $response = $field->getResource()->read($criteria);
131
132
        return !$response->hasErrors();
133
    }
134
}
135