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

Associate   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 8
dl 0
loc 64
ccs 0
cts 34
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B run() 0 39 6
A performAction() 0 4 1
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 Associate extends Action
23
{
24
    use ManageTrait,
25
        ResolverTrait;
26
27
    /**
28
     * @param string $field
29
     * @param string $source
30
     * @param string $target
31
     * @param int|null $siteId
32
     * @param int|null $sortOrder
33
     * @return mixed
34
     * @throws \yii\web\HttpException
35
     */
36
    public function run(
37
        string $field,
38
        string $source,
39
        string $target,
40
        int $siteId = null,
41
        int $sortOrder = null
42
    ) {
43
44
        // Resolve
45
        $field = $this->resolveField($field);
46
        $source = $this->resolveElement($source);
47
        $target = $this->resolveElement($target);
48
49
        /** @var Field $field */
50
51
        $siteId = SiteHelper::ensureSiteId($siteId ?: $source->siteId);
52
53
        $record = Association::find()
54
            ->fieldId($field->id)
55
            ->sourceId($source->getId() ?: false)
56
            ->targetId($target->getId() ?: false)
57
            ->siteId($siteId)
58
            ->one();
59
60
        if (!$record) {
61
            $record = new Association([
62
                'fieldId' => $field->id,
63
                'sourceId' => $source->getId(),
64
                'targetId' => $target->getId(),
65
                'siteId' => $siteId
66
            ]);
67
        }
68
69
        if ($sortOrder) {
70
            $record->sortOrder = $sortOrder;
0 ignored issues
show
Documentation introduced by
The property sortOrder does not exist on object<flipbox\craft\ele...ts\records\Association>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
71
        }
72
73
        return $this->runInternal($record);
74
    }
75
76
    /**
77
     * @inheritdoc
78
     * @param Association $record
79
     * @throws \Exception
80
     */
81
    protected function performAction(Association $record): bool
82
    {
83
        return $record->save();
84
    }
85
}
86