Completed
Push — master ( 04ac75...81967b )
by Nate
17:06
created

ProviderSettings   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 6
dl 0
loc 98
ccs 0
cts 54
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 21 1
A resolveSettings() 0 4 1
A resolveSettingsClass() 0 20 3
A isSettingsInstance() 0 4 2
A createSettings() 0 17 3
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