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\services; |
10
|
|
|
|
11
|
|
|
use craft\helpers\ArrayHelper; |
12
|
|
|
use craft\helpers\Json; |
13
|
|
|
use flipbox\ember\helpers\ModelHelper; |
14
|
|
|
use Flipbox\OAuth2\Client\Provider\Guardian as GuardianProvider; |
15
|
|
|
use flipbox\patron\events\RegisterProviderSettings; |
16
|
|
|
use flipbox\patron\providers\Base; |
17
|
|
|
use flipbox\patron\providers\Facebook as FacebookSettings; |
18
|
|
|
use flipbox\patron\providers\Guardian as GuardianSettings; |
19
|
|
|
use flipbox\patron\providers\SettingsInterface; |
20
|
|
|
use flipbox\patron\records\Provider; |
21
|
|
|
use League\OAuth2\Client\Provider\Facebook as FacebookProvider; |
22
|
|
|
use yii\base\Component; |
23
|
|
|
use yii\base\InvalidConfigException; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @author Flipbox Factory <[email protected]> |
27
|
|
|
* @since 1.0.0 |
28
|
|
|
*/ |
29
|
|
|
class ProviderSettings extends Component |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @inheritdoc |
33
|
|
|
*/ |
34
|
|
|
public function init() |
35
|
|
|
{ |
36
|
|
|
parent::init(); |
37
|
|
|
|
38
|
|
|
// Register setting handlers for providers |
39
|
|
|
RegisterProviderSettings::on( |
40
|
|
|
GuardianProvider::class, |
41
|
|
|
RegisterProviderSettings::REGISTER_SETTINGS, |
42
|
|
|
function (RegisterProviderSettings $event) { |
43
|
|
|
$event->class = GuardianSettings::class; |
44
|
|
|
} |
45
|
|
|
); |
46
|
|
|
|
47
|
|
|
RegisterProviderSettings::on( |
48
|
|
|
FacebookProvider::class, |
49
|
|
|
RegisterProviderSettings::REGISTER_SETTINGS, |
50
|
|
|
function (RegisterProviderSettings $event) { |
51
|
|
|
$event->class = FacebookSettings::class; |
52
|
|
|
} |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param Provider $provider |
58
|
|
|
* @param array $settings |
59
|
|
|
* @return SettingsInterface |
60
|
|
|
* @throws InvalidConfigException |
61
|
|
|
*/ |
62
|
|
|
public function resolveSettings(Provider $provider, $settings = []): SettingsInterface |
63
|
|
|
{ |
64
|
|
|
return $this->createSettings($provider->class, $settings); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $providerClass |
69
|
|
|
* @return mixed |
70
|
|
|
*/ |
71
|
|
|
protected function resolveSettingsClass(string $providerClass = null): string |
72
|
|
|
{ |
73
|
|
|
if (null === $providerClass) { |
74
|
|
|
return Base::class; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$event = new RegisterProviderSettings(); |
78
|
|
|
|
79
|
|
|
RegisterProviderSettings::trigger( |
80
|
|
|
$providerClass, |
81
|
|
|
RegisterProviderSettings::REGISTER_SETTINGS, |
82
|
|
|
$event |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
if (!$this->isSettingsInstance($event->class)) { |
86
|
|
|
return Base::class; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $event->class; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Check settings instance |
94
|
|
|
* |
95
|
|
|
* @param $class |
96
|
|
|
* @return bool |
97
|
|
|
*/ |
98
|
|
|
private function isSettingsInstance($class): bool |
99
|
|
|
{ |
100
|
|
|
return $class instanceof SettingsInterface || is_subclass_of($class, SettingsInterface::class); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param $providerClass |
105
|
|
|
* @param array $settings |
106
|
|
|
* @return SettingsInterface |
107
|
|
|
* @throws InvalidConfigException |
108
|
|
|
*/ |
109
|
|
|
protected function createSettings($providerClass, $settings = []): SettingsInterface |
110
|
|
|
{ |
111
|
|
|
if (is_string($settings)) { |
112
|
|
|
$settings = Json::decodeIfJson($settings); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if (!is_array($settings)) { |
116
|
|
|
$settings = ArrayHelper::toArray($settings, [], true); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$settings['class'] = $this->resolveSettingsClass($providerClass); |
120
|
|
|
|
121
|
|
|
/** @var SettingsInterface $model */ |
122
|
|
|
$model = ModelHelper::create($settings, SettingsInterface::class); |
123
|
|
|
|
124
|
|
|
return $model; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|