Completed
Push — develop ( 2f50d2...a740b5 )
by Nate
04:32
created

ElementListTrait::afterElementSave()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 15
cp 0
rs 9.2728
c 0
b 0
f 0
cc 5
nc 6
nop 2
crap 30
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\fields;
10
11
use Craft;
12
use craft\base\Element;
13
use craft\base\ElementInterface;
14
use craft\events\FieldElementEvent;
15
use flipbox\craft\element\lists\relationships\RelationshipInterface;
16
17
/**
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 2.0.0
20
 *
21
 * @property string $settingsTemplate
22
 */
23
trait ElementListTrait
24
{
25
    use ModifyElementQueryTrait,
26
        NormalizeValueTrait,
27
        InputTrait;
28
29
    /**
30
     * @var bool
31
     */
32
    protected $ignoreSearchKeywords = true;
33
34
    /**
35
     * @inheritdoc
36
     */
37
    public function getSearchKeywords($value, ElementInterface $element): string
38
    {
39
        if ($this->ignoreSearchKeywords === true) {
40
            return '';
41
        }
42
43
        return parent::getSearchKeywords($value, $element);
44
    }
45
46
    /**
47
     * Identify whether a sort order should be enforced.
48
     *
49
     * @return bool
50
     */
51
    public function ensureSortOrder(): bool
52
    {
53
        return $this->sortable;
0 ignored issues
show
Bug introduced by
The property sortable does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
54
    }
55
56
    /**
57
     * Allow the settings to identify whether the element should be sortable
58
     *
59
     * @param bool $sortable
60
     * @return $this
61
     */
62
    public function setSortable(bool $sortable = null)
63
    {
64
        $this->sortable = $sortable === true;
65
        return $this;
66
    }
67
68
    /**
69
     * Get the sortable attribute value
70
     *
71
     * @return bool
72
     */
73
    public function getSortable(): bool
74
    {
75
        return $this->sortable;
76
    }
77
78
    /**
79
     * @inheritDoc
80
     * @throws \Twig\Error\LoaderError
81
     * @throws \Twig\Error\RuntimeError
82
     * @throws \Twig\Error\SyntaxError
83
     */
84
    public function getSettingsHtml()
85
    {
86
        return Craft::$app->getView()->renderTemplate(
87
            'element-lists/_components/fieldtypes/settings',
88
            [
89
                'settingsTemplate' => $this->settingsTemplate,
90
                'field' => $this,
91
            ]
92
        );
93
    }
94
95
    /**
96
     * Our value is not an ElementQueryInterface and therefore we should handle it
97
     * differently.
98
     *
99
     * @inheritdoc
100
     */
101
    public function afterElementSave(ElementInterface $element, bool $isNew)
102
    {
103
        // Skip if the element is just propagating, and we're not localizing relations
104
        /** @var Element $element */
105
        if (!$element->propagating || $this->localizeRelations) {
0 ignored issues
show
Bug introduced by
The property localizeRelations does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
106
            /** @var RelationshipInterface $value */
107
            $value = $element->getFieldValue($this->handle);
108
109
            if ($value->isMutated()) {
110
                $value->save();
111
            }
112
        }
113
114
        // Trigger an 'afterElementSave' event
115
        if ($this->hasEventHandlers(self::EVENT_AFTER_ELEMENT_SAVE)) {
0 ignored issues
show
Bug introduced by
It seems like hasEventHandlers() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
116
            $this->trigger(self::EVENT_AFTER_ELEMENT_SAVE, new FieldElementEvent([
0 ignored issues
show
Bug introduced by
It seems like trigger() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
117
                'element' => $element,
118
                'isNew' => $isNew,
119
            ]));
120
        }
121
    }
122
}
123