Completed
Push — master ( 8ae47d...5423c6 )
by Nate
17:42 queued 15:46
created

Associate::validate()   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 flipbox\ember\helpers\SiteHelper;
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
        $element = $this->resolveElement($element);
55
56
        // Find existing?
57
        if (!empty($objectId)) {
58
            $association = HubSpot::getInstance()->getObjectAssociations()->getByCondition([
59
                'objectId' => $objectId,
60
                'elementId' => $element->getId(),
61
                'fieldId' => $field->id,
62
                'siteId' => SiteHelper::ensureSiteId($siteId ?: $element->siteId),
63
            ]);
64
        } else {
65
            $association = HubSpot::getInstance()->getObjectAssociations()->create([
66
                'elementId' => $element->getId(),
67
                'fieldId' => $field->id,
68
                'siteId' => SiteHelper::ensureSiteId($siteId ?: $element->siteId),
69
            ]);
70
        }
71
72
        $association->objectId = $newObjectId;
73
        $association->sortOrder = $sortOrder;
74
75
        return $this->runInternal($association);
76
    }
77
78
    /**
79
     * @inheritdoc
80
     * @param ObjectAssociation $model
81
     * @throws \flipbox\ember\exceptions\RecordNotFoundException
82
     * @throws \Exception
83
     */
84
    protected function performAction(Model $model): bool
85
    {
86
        if (true === $this->ensureAssociation($model)) {
87
            if ($this->validate === true && !$this->validate($model)) {
1 ignored issue
show
Compatibility introduced by
$model of type object<yii\base\Model> is not a sub-type of object<flipbox\hubspot\records\ObjectAssociation>. It seems like you assume a child class of the class yii\base\Model to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
88
                return false;
89
            }
90
91
            return HubSpot::getInstance()->getObjectAssociations()->associate(
92
                $model
93
            );
94
        }
95
96
        return false;
97
    }
98
99
    /**
100
     * @param ObjectAssociation $record
101
     * @return bool
102
     * @throws \Exception
103
     */
104
    protected function validate(
105
        ObjectAssociation $record
106
    ): bool {
107
108
        if (null === ($fieldId = $record->fieldId)) {
109
            return false;
110
        }
111
112
        if (null === ($field = HubSpot::getInstance()->getObjectsField()->findById($fieldId))) {
113
            return false;
114
        }
115
116
        $criteria = $field->getResource()->getAccessorCriteria([
117
            'id' => $record->objectId,
118
            'transformer' => TransformerCollection::class
119
        ]);
120
121
        /** @var DynamicModel $response */
122
        $response = $field->getResource()->read($criteria);
123
124
        return !$response->hasErrors();
125
    }
126
}
127