Completed
Push — develop ( c473d5...a8f522 )
by Nate
02:18
created

Dissociate::run()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 0
cts 22
cp 0
rs 8.8017
c 0
b 0
f 0
cc 6
nc 4
nop 4
crap 42
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-element-lists/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-element-lists/
7
 */
8
9
namespace flipbox\craft\element\lists\actions\source;
10
11
use craft\base\Field;
12
use flipbox\craft\ember\actions\ManageTrait;
13
use flipbox\craft\ember\helpers\SiteHelper;
14
use flipbox\craft\element\lists\actions\ResolverTrait;
15
use flipbox\craft\element\lists\records\Association;
16
use yii\base\Action;
17
18
/**
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 2.0.0
21
 */
22
class Dissociate 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 $source
35
     * @param string $target
36
     * @param int|null $siteId
37
     * @return mixed
38
     * @throws \yii\web\HttpException
39
     */
40
    public function run(
41
        string $field,
42
        string $source,
43
        string $target,
44
        int $siteId = null
45
    ) {
46
47
        // Resolve
48
        $field = $this->resolveField($field);
49
        $source = $this->resolveElement($source);
50
        $target = $this->resolveElement($target);
51
52
        /** @var Field $field */
53
54
        $siteId = $this->resolveSiteId($siteId ?: $source->siteId);
55
56
        $query = Association::find()
57
            ->fieldId($field->id)
58
            ->sourceId($source->getId() ?: false)
59
            ->targetId($target->getId() ?: false);
60
61
        if ($siteId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $siteId of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
62
            $query->siteId($siteId);
63
        }
64
65
        if (!$record = $query->one()) {
66
            return true;
67
        }
68
69
        return $this->runInternal($record);
70
    }
71
72
    /**
73
     * @param Association $record
74
     * @return bool
75
     * @throws \Throwable
76
     * @throws \yii\db\StaleObjectException
77
     */
78
    protected function performAction(Association $record): bool
79
    {
80
        return $record->delete();
81
    }
82
}
83