UserGroupAttributeTrait::userGroup()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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\db\Query;
12
use craft\db\QueryAbortedException;
13
use craft\models\UserGroup;
14
use craft\records\UserGroup as UserGroupRecord;
15
use flipbox\craft\ember\helpers\QueryHelper;
16
17
/**
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 2.0.0
20
 */
21
trait UserGroupAttributeTrait
22
{
23
    /**
24
     * The user group(s) that the resulting organizations’ users must be in.
25
     *
26
     * @var string|string[]|int|int[]|UserGroup|UserGroup[]|null
27
     */
28
    public $userGroup;
29
30
    /**
31
     * @param string|string[]|int|int[]|UserGroup|UserGroup[]|null $value
32
     * @return static The query object
33
     */
34
    public function setUserGroup($value)
35
    {
36
        $this->userGroup = $value;
37
        return $this;
38
    }
39
40
    /**
41
     * @param string|string[]|int|int[]|UserGroup|UserGroup[]|null $value
42
     * @return static The query object
43
     */
44
    public function userGroup($value)
45
    {
46
        return $this->setUserGroup($value);
47
    }
48
49
    /**
50
     * @param string|string[]|int|int[]|UserGroup|UserGroup[]|null $value
51
     * @return static The query object
52
     */
53
    public function setUserGroupId($value)
54
    {
55
        return $this->setUserGroup($value);
56
    }
57
58
    /**
59
     * @param string|string[]|int|int[]|UserGroup|UserGroup[]|null $value
60
     * @return static The query object
61
     */
62
    public function userGroupId($value)
63
    {
64
        return $this->setUserGroup($value);
65
    }
66
67
    /**
68
     * @param $value
69
     * @return int
70
     * @throws QueryAbortedException
71
     */
72
    protected function parseUserGroupValue($value)
73
    {
74
        $return = QueryHelper::prepareParam(
75
            $value,
76
            function (string $handle) {
77
                $value = (new Query())
78
                    ->select(['id'])
79
                    ->from([UserGroupRecord::tableName()])
80
                    ->where(['handle' => $handle])
81
                    ->scalar();
82
                return empty($value) ? false : $value;
83
            }
84
        );
85
86
        if ($return !== null && empty($return)) {
87
            throw new QueryAbortedException();
88
        }
89
90
        return $return;
91
    }
92
}
93