OrganizationAttributeTrait   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 35
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setOrganization() 0 5 1
A organization() 0 4 1
A setOrganizationId() 0 4 1
A organizationId() 0 4 1
A parseOrganizationValue() 0 20 4
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/organization/license
6
 * @link       https://www.flipboxfactory.com/software/organization/
7
 */
8
9
namespace flipbox\organizations\queries;
10
11
use craft\db\Query;
12
use craft\db\QueryAbortedException;
13
use flipbox\craft\ember\helpers\QueryHelper;
14
use flipbox\organizations\elements\Organization;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.0.0
19
 */
20
trait OrganizationAttributeTrait
21
{
22
    /**
23
     * The organization(s) that the resulting types must have.
24
     *
25
     * @var string|string[]|int|int[]|Organization|Organization[]|null $value
26
     */
27
    public $organization;
28
29
    /**
30
     * @param string|string[]|int|int[]|Organization|Organization[]|null $value
31
     * @return self The query object itself
32
     */
33
    public function setOrganization($value)
34
    {
35
        $this->organization = $value;
36
        return $this;
37
    }
38
39
    /**
40
     * @param string|string[]|int|int[]|Organization|Organization[]|null $value
41
     * @return static The query object
42
     */
43
    public function organization($value)
44
    {
45
        return $this->setOrganization($value);
46
    }
47
48
    /**
49
     * @param string|string[]|int|int[]|Organization|Organization[]|null $value
50
     * @return $this
51
     */
52
    public function setOrganizationId($value)
53
    {
54
        return $this->setOrganization($value);
55
    }
56
57
    /**
58
     * @param string|string[]|int|int[]|Organization|Organization[]|null $value
59
     * @return self The query object itself
60
     */
61
    public function organizationId($value)
62
    {
63
        return $this->setOrganization($value);
64
    }
65
66
    /**
67
     * @param $value
68
     * @return int
69
     * @throws QueryAbortedException
70
     */
71
    protected function parseOrganizationValue($value)
72
    {
73
        $return = QueryHelper::prepareParam(
74
            $value,
75
            function (string $slug) {
76
                $value = (new Query())
77
                    ->select(['id'])
78
                    ->from(['{{%elements_sites}} elements_sites'])
79
                    ->where(['slug' => $slug])
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