Completed
Push — master ( a20c7b...d885ca )
by Nate
02:36
created

Dissociate::performAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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 = SiteHelper::ensureSiteId($siteId ?: $source->siteId);
55
56
        $record = Association::find()
57
            ->fieldId($field->id)
58
            ->sourceId($source->getId() ?: false)
59
            ->targetId($target->getId() ?: false)
60
            ->siteId($siteId)
61
            ->one();
62
63
        if (!$record) {
64
            return true;
65
        }
66
67
        return $this->runInternal($record);
68
    }
69
70
    /**
71
     * @param Association $record
72
     * @return bool
73
     * @throws \Throwable
74
     * @throws \yii\db\StaleObjectException
75
     */
76
    protected function performAction(Association $record): bool
77
    {
78
        return $record->delete();
79
    }
80
}
81