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

Dissociate::run()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 0
cts 20
cp 0
rs 9.488
c 0
b 0
f 0
cc 3
nc 3
nop 4
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 yii\base\Model;
15
use yii\web\HttpException;
16
17
/**
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 1.0.0
20
 */
21
class Dissociate extends AbstractAssociationAction
22
{
23
    /**
24
     * @param string $field
25
     * @param string $element
26
     * @param string $objectId
27
     * @param int|null $siteId
28
     * @return mixed
29
     * @throws HttpException
30
     */
31
    public function run(
32
        string $field,
33
        string $element,
34
        string $objectId,
35
        int $siteId = null
36
    ) {
37
        // Resolve Field
38
        $field = $this->resolveField($field);
39
40
        // Resolve Element
41
        if (null === ($sourceElement = Craft::$app->getElements()->getElementById($element))) {
42
            return $this->handleInvalidElementResponse($element);
43
        }
44
45
        // Resolve Site Id
46
        if (null === $siteId) {
47
            $siteId = Craft::$app->getSites()->currentSite->id;
48
        }
49
50
        // Find existing?
51
        return $this->runInternal(HubSpot::getInstance()->getObjectAssociations()->create([
52
            'objectId' => $objectId,
53
            'elementId' => $sourceElement->getId(),
54
            'fieldId' => $field->id,
55
            'siteId' => $siteId,
56
        ]));
57
    }
58
59
    /**
60
     * @inheritdoc
61
     * @param ObjectAssociation $model
62
     * @throws \flipbox\ember\exceptions\RecordNotFoundException
63
     * @throws \yii\db\Exception
64
     */
65
    protected function performAction(Model $model): bool
66
    {
67
        if (true === $this->ensureAssociation($model)) {
68
            return HubSpot::getInstance()->getObjectAssociations()->dissociate(
69
                $model
70
            );
71
        }
72
73
        return false;
74
    }
75
}
76