UserAttributeTrait::parseUserValue()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

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