TokenAttributesTrait::accessToken()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
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://flipboxfactory.com/software/patron/license
6
 * @link       https://www.flipboxfactory.com/patron/domains/
7
 */
8
9
namespace flipbox\patron\queries;
10
11
use craft\helpers\Db;
12
use yii\db\Expression;
13
14
/**
15
 * @author Flipbox Factory <[email protected]>
16
 * @since 1.0.0
17
 */
18
trait TokenAttributesTrait
19
{
20
    /**
21
     * @var bool|null The enabled state
22
     */
23
    public $enabled = true;
24
25
    /**
26
     * @var string|string[]|null The access token(s). Prefix IDs with "not " to exclude them.
27
     */
28
    public $accessToken;
29
30
    /**
31
     * @var string|string[]|null The refresh token(s). Prefix IDs with "not " to exclude them.
32
     */
33
    public $refreshToken;
34
35
    /**
36
     * Adds an additional WHERE condition to the existing one.
37
     * The new condition and the existing one will be joined using the `AND` operator.
38
     * @param string|array|Expression $condition the new WHERE condition. Please refer to [[where()]]
39
     * on how to specify this parameter.
40
     * @param array $params the parameters (name => value) to be bound to the query.
41
     * @return $this the query object itself
42
     * @see where()
43
     * @see orWhere()
44
     */
45
    abstract public function andWhere($condition, $params = []);
46
47
    /**
48
     * Appends a LEFT OUTER JOIN part to the query.
49
     * @param string|array $table the table to be joined.
50
     *
51
     * Use a string to represent the name of the table to be joined.
52
     * The table name can contain a schema prefix (e.g. 'public.user') and/or table alias (e.g. 'user u').
53
     * The method will automatically quote the table name unless it contains some parenthesis
54
     * (which means the table is given as a sub-query or DB expression).
55
     *
56
     * Use an array to represent joining with a sub-query. The array must contain only one element.
57
     * The value must be a [[Query]] object representing the sub-query while the corresponding key
58
     * represents the alias for the sub-query.
59
     *
60
     * @param string|array $on the join condition that should appear in the ON part.
61
     * Please refer to [[join()]] on how to specify this parameter.
62
     * @param array $params the parameters (name => value) to be bound to the query
63
     * @return $this the query object itself
64
     */
65
    abstract public function leftJoin($table, $on = '', $params = []);
66
67
    /**
68
     * @param $enabled
69
     * @return $this
70
     */
71
    public function enabled($enabled)
72
    {
73
        $this->enabled = $enabled;
74
        return $this;
75
    }
76
77
    /**
78
     * @param $accessToken
79
     * @return $this
80
     */
81
    public function accessToken($accessToken)
82
    {
83
        $this->accessToken = $accessToken;
84
        return $this;
85
    }
86
87
    /**
88
     * @param $refreshToken
89
     * @return $this
90
     */
91
    public function refreshToken($refreshToken)
92
    {
93
        $this->refreshToken = $refreshToken;
94
        return $this;
95
    }
96
97
    /**
98
     *
99
     */
100
    protected function applyTokenConditions()
101
    {
102
        if ($this->enabled !== null) {
103
            $this->andWhere(Db::parseParam('enabled', $this->enabled));
104
        }
105
106
        $attributes = ['accessToken', 'refreshToken'];
107
108
        foreach ($attributes as $attribute) {
109
            if (($value = $this->{$attribute}) !== null) {
110
                $this->andWhere(Db::parseParam($attribute, $value));
111
            }
112
        }
113
    }
114
}
115