Completed
Push — develop ( f37ae4...1d947e )
by Nate
18:30
created

Associate   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 6
dl 0
loc 110
ccs 0
cts 58
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 43 4
A performAction() 0 14 4
A validateResource() 0 19 3
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/force/license
6
 * @link       https://www.flipboxfactory.com/software/force/
7
 */
8
9
namespace flipbox\hubspot\actions\resources;
10
11
use Craft;
12
use flipbox\hubspot\HubSpot;
13
use flipbox\hubspot\records\ResourceAssociation;
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 Salesforce 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 $newHubSpotId
35
     * @param string|null $hubSpotId
36
     * @param int|null $siteId
37
     * @param int|null $sortOrder
38
     * @return void|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 $newHubSpotId,
46
        string $hubSpotId = 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($hubSpotId)) {
65
            $association = HubSpot::getInstance()->getResourceAssociations()->getByCondition([
66
                'hubSpotId' => $hubSpotId,
67
                'elementId' => $sourceElement->getId(),
68
                'fieldId' => $field->id,
69
                'siteId' => $siteId,
70
            ]);
71
        } else {
72
            $association = HubSpot::getInstance()->getResourceAssociations()->create([
73
                'elementId' => $sourceElement->getId(),
74
                'fieldId' => $field->id,
75
                'siteId' => $siteId,
76
77
            ]);
78
        }
79
80
        $association->hubSpotId = $newHubSpotId;
81
        $association->sortOrder = $sortOrder;
82
83
        return $this->runInternal($association);
84
    }
85
86
    /**
87
     * @inheritdoc
88
     * @param ResourceAssociation $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)) {
0 ignored issues
show
Compatibility introduced by
$model of type object<yii\base\Model> is not a sub-type of object<flipbox\hubspot\r...ds\ResourceAssociation>. 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...
96
                return false;
97
            }
98
99
            return HubSpot::getInstance()->getResourceAssociations()->associate(
100
                $model
101
            );
102
        }
103
104
        return false;
105
    }
106
107
    /**
108
     * @param ResourceAssociation $record
109
     * @return bool
110
     * @throws \Exception
111
     */
112
    protected function validateResource(
113
        ResourceAssociation $record
114
    ): bool {
115
116
        if (null === ($fieldId = $record->fieldId)) {
117
            return false;
118
        }
119
120
        if (null === ($field = HubSpot::getInstance()->getResourcesField()->findById($fieldId))) {
121
            return false;
122
        }
123
124
        $criteria = $field->createResourceCriteria($record);
125
126
        /** @var DynamicModel $response */
127
        $response = $criteria->fetch(['transformer' => TransformerCollection::class]);
128
129
        return !$response->hasErrors();
130
    }
131
}
132