|
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/software/patron/ |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace flipbox\patron\helpers; |
|
10
|
|
|
|
|
11
|
|
|
use craft\helpers\StringHelper; |
|
12
|
|
|
use flipbox\patron\records\Provider; |
|
13
|
|
|
use League\OAuth2\Client\Provider\AbstractProvider; |
|
14
|
|
|
use ReflectionClass; |
|
15
|
|
|
use yii\db\Query; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @author Flipbox Factory <[email protected]> |
|
19
|
|
|
* @since 1.0.0 |
|
20
|
|
|
*/ |
|
21
|
|
|
class ProviderHelper |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @param $provider |
|
25
|
|
|
* @return string |
|
26
|
|
|
* @throws \ReflectionException |
|
27
|
|
|
*/ |
|
28
|
|
|
public static function displayName($provider): string |
|
29
|
|
|
{ |
|
30
|
|
|
$reflect = new ReflectionClass( |
|
31
|
|
|
$provider |
|
32
|
|
|
); |
|
33
|
|
|
|
|
34
|
|
|
// Split capital letters |
|
35
|
|
|
$parts = preg_split("/(?<=[a-z])(?![a-z])/", $reflect->getShortName(), -1, PREG_SPLIT_NO_EMPTY); |
|
36
|
|
|
|
|
37
|
|
|
// Assemble |
|
38
|
|
|
return StringHelper::toString($parts, ' '); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param AbstractProvider $provider |
|
43
|
|
|
* @param array $properties |
|
44
|
|
|
* @return array |
|
45
|
|
|
* @throws \ReflectionException |
|
46
|
|
|
*/ |
|
47
|
|
|
public static function getProtectedProperties( |
|
48
|
|
|
AbstractProvider $provider, |
|
49
|
|
|
array $properties |
|
50
|
|
|
): array { |
|
51
|
|
|
$reflection = new ReflectionClass($provider); |
|
52
|
|
|
|
|
53
|
|
|
$values = []; |
|
54
|
|
|
|
|
55
|
|
|
foreach ($properties as $property) { |
|
56
|
|
|
$values[] = static::getProtectedProperty( |
|
57
|
|
|
$provider, |
|
58
|
|
|
$reflection, |
|
59
|
|
|
$property |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $values; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/******************************************* |
|
67
|
|
|
* GET ID |
|
68
|
|
|
*******************************************/ |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param AbstractProvider $provider |
|
72
|
|
|
* @return int|null |
|
73
|
|
|
* @throws \ReflectionException |
|
74
|
|
|
*/ |
|
75
|
|
|
public static function lookupId(AbstractProvider $provider) |
|
76
|
|
|
{ |
|
77
|
|
|
list($clientId, $clientSecret) = ProviderHelper::getProtectedProperties( |
|
78
|
|
|
$provider, |
|
79
|
|
|
['clientId', 'clientSecret'] |
|
80
|
|
|
); |
|
81
|
|
|
|
|
82
|
|
|
$condition = [ |
|
83
|
|
|
'class' => get_class($provider), |
|
84
|
|
|
'clientId' => $clientId, |
|
85
|
|
|
'clientSecret' => $clientSecret |
|
86
|
|
|
]; |
|
87
|
|
|
|
|
88
|
|
|
if (!$providerId = (new Query()) |
|
89
|
|
|
->select(['id']) |
|
90
|
|
|
->from([Provider::tableName() . ' ' . Provider::tableAlias()]) |
|
91
|
|
|
->where($condition) |
|
92
|
|
|
->scalar() |
|
93
|
|
|
) { |
|
94
|
|
|
return null; |
|
95
|
|
|
}; |
|
96
|
|
|
|
|
97
|
|
|
return (int)$providerId; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param AbstractProvider $provider |
|
103
|
|
|
* @param ReflectionClass $reflectionClass |
|
104
|
|
|
* @param string $property |
|
105
|
|
|
* @return mixed |
|
106
|
|
|
* @throws \ReflectionException |
|
107
|
|
|
*/ |
|
108
|
|
|
public static function getProtectedProperty( |
|
109
|
|
|
AbstractProvider $provider, |
|
110
|
|
|
ReflectionClass $reflectionClass, |
|
111
|
|
|
string $property |
|
112
|
|
|
) { |
|
113
|
|
|
$reflectionProperty = $reflectionClass->getProperty($property); |
|
114
|
|
|
$reflectionProperty->setAccessible(true); |
|
115
|
|
|
return $reflectionProperty->getValue($provider); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|