Completed
Push — develop ( 0ff0ed...a13565 )
by Nate
12:12
created

DissociateObject   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 6
dl 0
loc 52
c 0
b 0
f 0
ccs 0
cts 21
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 22 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\integration\actions\ResolverTrait;
13
use flipbox\craft\integration\records\IntegrationAssociation;
14
use yii\base\Action;
15
use yii\web\HttpException;
16
17
/**
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 1.0.0
20
 */
21
class DissociateObject extends Action
22
{
23
    use ManageTrait,
24
        ResolverTrait;
25
26
    /**
27
     * @var int
28
     */
29
    public $statusCodeSuccess = 201;
30
31
    /**
32
     * @param string $field
33
     * @param string $element
34
     * @param string $objectId
35
     * @param int|null $siteId
36
     * @return bool|mixed
37
     * @throws HttpException
38
     */
39
    public function run(
40
        string $field,
41
        string $element,
42
        string $objectId,
43
        int $siteId = null
44
    ) {
45
    
46
        $field = $this->resolveField($field);
47
        $element = $this->resolveElement($element);
48
49
        $query = $field->normalizeValue($objectId, $element);
50
51
        if (null !== $siteId) {
52
            $query->siteId($siteId);
53
        }
54
55
        if (null === ($association = $query->one())) {
56
            return $this->handleSuccessResponse(true);
57
        }
58
59
        return $this->runInternal($association);
60
    }
61
62
    /**
63
     * @param IntegrationAssociation $record
64
     * @return bool
65
     * @throws \Throwable
66
     * @throws \yii\db\StaleObjectException
67
     */
68
    protected function performAction(IntegrationAssociation $record): bool
69
    {
70
        return $record->delete();
71
    }
72
}
73