Completed
Push — master ( b96afd...2731ff )
by Nate
08:30 queued 06:24
created

OrganizationAttributeTrait::resolveOrganizationValue()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 13
cp 0
rs 9.4888
c 0
b 0
f 0
cc 5
nc 7
nop 2
crap 30
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 flipbox\craft\ember\helpers\QueryHelper;
13
use flipbox\organizations\elements\Organization;
14
15
/**
16
 * @author Flipbox Factory <[email protected]>
17
 * @since 1.0.0
18
 */
19
trait OrganizationAttributeTrait
20
{
21
    /**
22
     * The organization(s) that the resulting types must have.
23
     *
24
     * @var string|string[]|int|int[]|Organization|Organization[]|null $value
25
     */
26
    public $organization;
27
28
    /**
29
     * @param string|string[]|int|int[]|Organization|Organization[]|null $value
30
     * @return self The query object itself
31
     */
32
    public function setOrganization($value)
33
    {
34
        $this->organization = $value;
35
        return $this;
36
    }
37
38
    /**
39
     * @param string|string[]|int|int[]|Organization|Organization[]|null $value
40
     * @return static The query object
41
     */
42
    public function organization($value)
43
    {
44
        return $this->setOrganization($value);
45
    }
46
47
    /**
48
     * @param string|string[]|int|int[]|Organization|Organization[]|null $value
49
     * @return $this
50
     */
51
    public function setOrganizationId($value)
52
    {
53
        return $this->setOrganization($value);
54
    }
55
56
    /**
57
     * @param string|string[]|int|int[]|Organization|Organization[]|null $value
58
     * @return self The query object itself
59
     */
60
    public function organizationId($value)
61
    {
62
        return $this->setOrganization($value);
63
    }
64
65
    /**
66
     * @param $value
67
     * @return array|string
68
     */
69
    protected function parseOrganizationValue($value)
70
    {
71
        return QueryHelper::prepareParam(
72
            $value,
73
            function (string $slug) {
74
                $value = (new Query())
75
                    ->select(['id'])
76
                    ->from(['{{%elements_sites}} elements_sites'])
77
                    ->where(['slug' => $slug])
78
                    ->scalar();
79
                return empty($value) ? false : $value;
80
            }
81
        );
82
    }
83
}
84