parseOrganizationValue()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 0
cts 18
cp 0
rs 9.6
c 0
b 0
f 0
cc 4
nc 2
nop 1
crap 20
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