Completed
Branch develop (cd8f66)
by Nate
03:44
created

UserGroupAttribute   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 6
dl 0
loc 104
ccs 0
cts 53
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUserGroup() 0 5 1
A userGroup() 0 4 1
A setUserGroupId() 0 4 1
A userGroupId() 0 4 1
A parseUserGroupValue() 0 17 4
B resolveUserGroupValue() 0 16 5
A resolveUserGroupStringValue() 0 9 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\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))
1 ignored issue
show
Bug introduced by
It seems like \craft\helpers\Db::parseParam('handle', $value) targeting craft\helpers\Db::parseParam() can also be of type string; however, craft\db\Query::where() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
122
            ->scalar();
123
        return empty($value) ? false : $value;
124
    }
125
}
126