Completed
Branch develop (9e5d0f)
by Nate
01:59
created

UserGroupQueryValue   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 6
dl 0
loc 59
ccs 0
cts 35
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A parseUserGroupValue() 0 17 4
B resolveUserGroupValue() 0 16 5
A resolveUserGroupStringValue() 0 8 1
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\helpers\traits;
10
11
use craft\db\Query;
12
use craft\helpers\Db;
13
use craft\models\UserGroup;
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 UserGroupQueryValue
23
{
24
    /**
25
     * @param $value
26
     * @param string $join
27
     * @return array
28
     */
29
    public static function parseUserGroupValue($value, string $join = 'and'): array
30
    {
31
        if (false === QueryHelper::parseBaseParam($value, $join)) {
32
            foreach ($value as $operator => &$v) {
33
                self::resolveUserGroupValue($operator, $v);
34
            }
35
        }
36
37
        $value = ArrayHelper::filterEmptyAndNullValuesFromArray($value);
38
39
        if (empty($value)) {
40
            return [];
41
        }
42
43
        // parse param to allow for mixed variables
44
        return array_merge([$join], $value);
45
    }
46
47
    /**
48
     * @param $operator
49
     * @param $value
50
     */
51
    private static function resolveUserGroupValue($operator, &$value)
52
    {
53
        if (false === QueryHelper::findParamValue($value, $operator)) {
54
            if (is_string($value)) {
55
                $value = static::resolveUserGroupStringValue($value);
56
            }
57
58
            if ($value instanceof UserGroup) {
59
                $value = $value->id;
60
            }
61
62
            if ($value) {
63
                $value = QueryHelper::assembleParamValue($value, $operator);
64
            }
65
        }
66
    }
67
68
    /**
69
     * @param string $value
70
     * @return int|null
71
     */
72
    protected static function resolveUserGroupStringValue(string $value)
73
    {
74
        return (new Query())
75
            ->select(['id'])
76
            ->from([UserGroupRecord::tableName()])
77
            ->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...
78
            ->scalar();
79
    }
80
}
81