Completed
Push — master ( e3199e...dd66f1 )
by Nate
08:31 queued 06:54
created

UserGroupAttribute::resolveUserGroupValue()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 14
cp 0
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 8
nc 9
nop 2
crap 30
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\ember\db\traits;
10
11
use craft\models\UserGroup;
12
use craft\db\Query;
13
use craft\helpers\Db;
14
use flipbox\ember\helpers\ArrayHelper;
15
use flipbox\ember\helpers\QueryHelper;
16
use craft\records\UserGroup as UserGroupRecord;
17
18
/**
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 1.0.0
21
 */
22
trait UserGroupAttribute
23
{
24
    /**
25
     * The user group(s) that the resulting organizations’ users must be in.
26
     *
27
     * @var string|string[]|int|int[]|UserGroup|UserGroup[]|null
28
     */
29
    public $userGroup;
30
31
    /**
32
     * @param string|string[]|int|int[]|UserGroup|UserGroup[]|null $value
33
     * @return static The query object
34
     */
35
    public function setUserGroup($value)
36
    {
37
        $this->userGroup = $value;
38
        return $this;
39
    }
40
41
    /**
42
     * @param string|string[]|int|int[]|UserGroup|UserGroup[]|null $value
43
     * @return static The query object
44
     */
45
    public function userGroup($value)
46
    {
47
        return $this->setUserGroup($value);
48
    }
49
50
    /**
51
     * @param string|string[]|int|int[]|UserGroup|UserGroup[]|null $value
52
     * @return static The query object
53
     */
54
    public function setUserGroupId($value)
55
    {
56
        return $this->setUserGroup($value);
57
    }
58
59
    /**
60
     * @param string|string[]|int|int[]|UserGroup|UserGroup[]|null $value
61
     * @return static The query object
62
     */
63
    public function userGroupId($value)
64
    {
65
        return $this->setUserGroup($value);
66
    }
67
68
    /**
69
     * @param $value
70
     * @param string $join
71
     * @return array
72
     */
73
    public function parseUserGroupValue($value, string $join = 'or'): array
74
    {
75
        if (false === QueryHelper::parseBaseParam($value, $join)) {
76
            foreach ($value as $operator => &$v) {
77
                $this->resolveUserGroupValue($operator, $v);
78
            }
79
        }
80
81
        $value = ArrayHelper::filterEmptyAndNullValuesFromArray($value);
82
83
        if (empty($value)) {
84
            return [];
85
        }
86
87
        // parse param to allow for mixed variables
88
        return array_merge([$join], $value);
89
    }
90
91
    /**
92
     * @param $operator
93
     * @param $value
94
     */
95
    private function resolveUserGroupValue($operator, &$value)
96
    {
97
        if (false === QueryHelper::findParamValue($value, $operator)) {
98
            if (is_string($value)) {
99
                $value = $this->resolveUserGroupStringValue($value);
100
            }
101
102
            if ($value instanceof UserGroup) {
103
                $value = $value->id;
104
            }
105
106
            if ($value) {
107
                $value = QueryHelper::assembleParamValue($value, $operator);
108
            }
109
        }
110
    }
111
112
    /**
113
     * @param string $value
114
     * @return string|null
115
     */
116
    protected function resolveUserGroupStringValue(string $value)
117
    {
118
        $value = (new Query())
119
            ->select(['id'])
120
            ->from([UserGroupRecord::tableName()])
121
            ->where(Db::parseParam('handle', $value))
122
            ->scalar();
123
        return empty($value) ? false : $value;
124
    }
125
}
126