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 flipbox\craft\ember\helpers\QueryHelper; |
13
|
|
|
use flipbox\patron\helpers\ProviderHelper; |
14
|
|
|
use flipbox\patron\records\Provider as ProviderRecord; |
15
|
|
|
use flipbox\patron\records\Token; |
16
|
|
|
use League\OAuth2\Client\Provider\AbstractProvider; |
17
|
|
|
use yii\db\Query; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Flipbox Factory <[email protected]> |
21
|
|
|
* @since 1.0.0 |
22
|
|
|
*/ |
23
|
|
|
trait TokenProviderAttributeTrait |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* The provider(s) that the resulting must have. |
27
|
|
|
* |
28
|
|
|
* @var string|string[]|int|int[]|ProviderRecord|ProviderRecord[]|AbstractProvider|AbstractProvider[]|null |
29
|
|
|
*/ |
30
|
|
|
public $provider; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string|string[]|int|int[]|ProviderRecord|ProviderRecord[]|AbstractProvider|AbstractProvider[]|null $value |
34
|
|
|
* @return static The query object |
35
|
|
|
*/ |
36
|
|
|
public function setProvider($value) |
37
|
|
|
{ |
38
|
|
|
$this->provider = $value; |
39
|
|
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string|string[]|int|int[]|ProviderRecord|ProviderRecord[]|AbstractProvider|AbstractProvider[]|null $value |
44
|
|
|
* @return static The query object |
45
|
|
|
*/ |
46
|
|
|
public function provider($value) |
47
|
|
|
{ |
48
|
|
|
return $this->setProvider($value); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string|string[]|int|int[]|ProviderRecord|ProviderRecord[]|AbstractProvider|AbstractProvider[]|null $value |
53
|
|
|
* @return static The query object |
54
|
|
|
*/ |
55
|
|
|
public function setProviderId($value) |
56
|
|
|
{ |
57
|
|
|
return $this->setProvider($value); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string|string[]|int|int[]|ProviderRecord|ProviderRecord[]|AbstractProvider|AbstractProvider[]|null $value |
62
|
|
|
* @return static The query object |
63
|
|
|
*/ |
64
|
|
|
public function providerId($value) |
65
|
|
|
{ |
66
|
|
|
return $this->setProvider($value); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param $value |
71
|
|
|
* @param string $join |
72
|
|
|
* @return array |
73
|
|
|
* @throws \ReflectionException |
74
|
|
|
*/ |
75
|
|
|
protected function parseProviderValue($value, string $join = 'or'): array |
76
|
|
|
{ |
77
|
|
|
if (false === QueryHelper::parseBaseParam($value, $join)) { |
78
|
|
|
foreach ($value as $operator => &$v) { |
79
|
|
|
$this->resolveProviderValue($operator, $v); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// Filter null and empties |
84
|
|
|
$value = array_filter($value, function ($arr): bool { |
85
|
|
|
return $arr !== null && $arr !== ''; |
86
|
|
|
}); |
87
|
|
|
|
88
|
|
|
if (empty($value)) { |
89
|
|
|
return []; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return array_merge([$join], $value); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param $operator |
97
|
|
|
* @param $value |
98
|
|
|
* @throws \ReflectionException |
99
|
|
|
*/ |
100
|
|
|
protected function resolveProviderValue($operator, &$value) |
101
|
|
|
{ |
102
|
|
|
if ($value instanceof AbstractProvider) { |
103
|
|
|
$value = ProviderHelper::lookupId($value); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (false === QueryHelper::findParamValue($value, $operator)) { |
107
|
|
|
if (is_string($value)) { |
108
|
|
|
$value = $this->resolveProviderStringValue($value); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if ($value instanceof ProviderRecord) { |
112
|
|
|
$value = $value->id; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if ($value) { |
116
|
|
|
$value = QueryHelper::assembleParamValue($value, $operator); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param string $value |
123
|
|
|
* @return int|null |
124
|
|
|
*/ |
125
|
|
|
protected function resolveProviderStringValue(string $value) |
126
|
|
|
{ |
127
|
|
|
if (is_numeric($value)) { |
128
|
|
|
$condition = ['id' => $value]; |
129
|
|
|
} else { |
130
|
|
|
$condition = ['handle' => $value]; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$id = (new Query()) |
134
|
|
|
->select(['id']) |
135
|
|
|
->from([ProviderRecord::tableName()]) |
136
|
|
|
->where($condition) |
137
|
|
|
->scalar(); |
138
|
|
|
|
139
|
|
|
return $id ? (int)$id : null; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @throws \ReflectionException |
144
|
|
|
*/ |
145
|
|
|
protected function applyProviderConditions() |
146
|
|
|
{ |
147
|
|
|
if (empty($this->provider)) { |
148
|
|
|
return; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$this->andWhere( |
|
|
|
|
152
|
|
|
Db::parseParam(Token::tableAlias() . '.providerId', $this->parseProviderValue($this->provider)) |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.