ElementAttributeTrait   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 72
ccs 0
cts 29
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setElement() 0 5 1
A element() 0 4 1
A setElementId() 0 4 1
A elementId() 0 4 1
A parseElementValue() 0 20 4
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-ember/
7
 */
8
9
namespace flipbox\craft\ember\queries;
10
11
use craft\base\ElementInterface;
12
use craft\db\Query;
13
use craft\db\QueryAbortedException;
14
use flipbox\craft\ember\helpers\QueryHelper;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 2.0.0
19
 */
20
trait ElementAttributeTrait
21
{
22
    /**
23
     * The element(s) that the resulting organizations’ elements must have.
24
     *
25
     * @var string|string[]|int|int[]|ElementInterface|ElementInterface[]|null
26
     */
27
    public $element;
28
29
    /**
30
     * @param string|string[]|int|int[]|ElementInterface|ElementInterface[]|null $value
31
     * @return static The query object
32
     */
33
    public function setElement($value)
34
    {
35
        $this->element = $value;
36
        return $this;
37
    }
38
39
    /**
40
     * @param string|string[]|int|int[]|ElementInterface|ElementInterface[]|null $value
41
     * @return static The query object
42
     */
43
    public function element($value)
44
    {
45
        return $this->setElement($value);
46
    }
47
48
    /**
49
     * @param string|string[]|int|int[]|ElementInterface|ElementInterface[]|null $value
50
     * @return static The query object
51
     */
52
    public function setElementId($value)
53
    {
54
        return $this->setElement($value);
55
    }
56
57
    /**
58
     * @param string|string[]|int|int[]|ElementInterface|ElementInterface[]|null $value
59
     * @return static The query object
60
     */
61
    public function elementId($value)
62
    {
63
        return $this->setElement($value);
64
    }
65
66
    /**
67
     * @param $value
68
     * @return int
69
     * @throws QueryAbortedException
70
     */
71
    protected function parseElementValue($value)
72
    {
73
        $return = QueryHelper::prepareParam(
74
            $value,
75
            function (string $uri) {
76
                $value = (new Query())
77
                    ->select(['id'])
78
                    ->from(['{{%elements_sites}} elements_sites'])
79
                    ->where(['uri' => $uri])
80
                    ->scalar();
81
                return empty($value) ? false : $value;
82
            }
83
        );
84
85
        if ($return !== null && empty($return)) {
86
            throw new QueryAbortedException();
87
        }
88
89
        return $return;
90
    }
91
}
92