DissociateObject   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 7
dl 0
loc 55
ccs 0
cts 23
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 25 3
A performAction() 0 4 1
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-integration/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-integration/
7
 */
8
9
namespace flipbox\craft\integration\actions\objects;
10
11
use flipbox\craft\ember\actions\ManageTrait;
12
use flipbox\craft\ember\helpers\SiteHelper;
13
use flipbox\craft\integration\actions\ResolverTrait;
14
use flipbox\craft\integration\records\IntegrationAssociation;
15
use yii\base\Action;
16
use yii\web\HttpException;
17
18
/**
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 2.0.0
21
 */
22
class DissociateObject extends Action
23
{
24
    use ManageTrait,
25
        ResolverTrait;
26
27
    /**
28
     * @var int
29
     */
30
    public $statusCodeSuccess = 201;
31
32
    /**
33
     * @param string $field
34
     * @param string $element
35
     * @param string $objectId
36
     * @param int|null $siteId
37
     * @return bool|mixed
38
     * @throws HttpException
39
     */
40
    public function run(
41
        string $field,
42
        string $element,
43
        string $objectId,
44
        int $siteId = null
45
    ) {
46
47
        $field = $this->resolveField($field);
48
        $element = $this->resolveElement($element);
49
50
        /** @var IntegrationAssociation $recordClass */
51
        $recordClass = $field::recordClass();
52
53
        $query = $recordClass::find();
54
        $query->setElement($element)
55
            ->setField($field)
56
            ->setObjectId($objectId)
57
            ->setSiteId(SiteHelper::ensureSiteId($siteId ?: $element->siteId));
58
59
        if (null === ($association = $query->one())) {
60
            return $this->handleSuccessResponse(true);
61
        }
62
63
        return $this->runInternal($association);
64
    }
65
66
    /**
67
     * @param IntegrationAssociation $record
68
     * @return bool
69
     * @throws \Throwable
70
     * @throws \yii\db\StaleObjectException
71
     */
72
    protected function performAction(IntegrationAssociation $record): bool
73
    {
74
        return $record->delete();
75
    }
76
}
77