Completed
Push — master ( 19b2ae...0e0de4 )
by Nate
04:43
created

ProviderQuery   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 10

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 14
lcom 2
cbo 10
dl 0
loc 144
ccs 0
cts 67
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 17 2
A populate() 0 12 3
A one() 0 8 2
A createObject() 0 12 1
A prepareConfig() 0 17 2
A extractSettings() 0 15 3
A prepare() 0 7 1
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\queries;
10
11
use craft\db\Query;
12
use craft\db\QueryAbortedException;
13
use craft\helpers\ArrayHelper;
14
use craft\helpers\Json;
15
use flipbox\craft\ember\helpers\ObjectHelper;
16
use flipbox\craft\ember\queries\AuditAttributesTrait;
17
use flipbox\patron\helpers\ProviderHelper;
18
use flipbox\patron\Patron;
19
use flipbox\patron\records\Provider;
20
use flipbox\patron\records\ProviderInstance;
21
use League\OAuth2\Client\Provider\AbstractProvider;
22
23
/**
24
 * @author Flipbox Factory <[email protected]>
25
 * @since 1.0.0
26
 */
27
class ProviderQuery extends Query
28
{
29
    use ProviderAttributesTrait,
30
        AuditAttributesTrait;
31
32
    /**
33
     * @inheritdoc
34
     */
35
    public $orderBy = [
36
        'enabled' => SORT_DESC,
37
        'dateUpdated' => SORT_DESC
38
    ];
39
40
    /**
41
     * @inheritdoc
42
     */
43
    public function init()
44
    {
45
        $this->from = [Provider::tableName() . ' ' . Provider::tableAlias()];
46
47
        $this->select = [
48
            Provider::tableAlias() . '.*',
49
            ProviderInstance::tableAlias() . '.clientId',
50
            ProviderInstance::tableAlias() . '.clientSecret',
51
            ProviderInstance::tableAlias() . '.settings'
52
        ];
53
54
        parent::init();
55
56
        if ($this->environment === null) {
57
            $this->environment = Patron::getInstance()->getSettings()->getEnvironment();
58
        }
59
    }
60
61
    /**
62
     * @inheritdoc
63
     * @throws \yii\base\InvalidConfigException
64
     */
65
    public function populate($rows)
66
    {
67
        $results = parent::populate($rows);
68
69
        if (Patron::getInstance()->getSettings()->getEncryptStorageData() === true) {
70
            foreach ($results as $key => $result) {
71
                $results[$key] = $this->createObject($result, false);
72
            }
73
        }
74
75
        return $results;
76
    }
77
78
    /**
79
     * @inheritdoc
80
     * @return AbstractProvider
81
     * @throws \yii\base\InvalidConfigException
82
     */
83
    public function one($db = null)
84
    {
85
        if (null === ($config = parent::one($db))) {
86
            return null;
87
        }
88
89
        return $this->createObject($config);
90
    }
91
92
    /**
93
     * @param array $config
94
     * @param bool $checkSettings
95
     * @return mixed
96
     * @throws \yii\base\InvalidConfigException
97
     */
98
    protected function createObject(array $config, bool $checkSettings = true)
99
    {
100
        $config = $this->prepareConfig($config, $checkSettings);
101
102
        // Provider class
103
        $class = ObjectHelper::checkConfig(
104
            $config,
105
            AbstractProvider::class
106
        );
107
108
        return new $class($config);
109
    }
110
111
    /**
112
     * @param array $config
113
     * @param bool $checkSettings
114
     * @return array
115
     */
116
    protected function prepareConfig(array $config = [], bool $checkSettings = true): array
117
    {
118
        // Extract 'clientSecret'
119
        $clientSecret = ArrayHelper::remove($config, 'clientSecret');
120
121
        if (!empty($clientSecret)) {
122
            $config['clientSecret'] = ProviderHelper::decryptClientSecret($clientSecret, $checkSettings);
123
        }
124
125
        // Merge in settings
126
        $config = array_merge($config, $this->extractSettings($config));
127
128
        // This doesn't change
129
        $config['redirectUri'] = Patron::getInstance()->getSettings()->getCallbackUrl();
130
131
        return $config;
132
    }
133
134
    /**
135
     * @param array $config
136
     * @return array
137
     */
138
    protected function extractSettings(array &$config): array
139
    {
140
        // We could init the SettingsInterface and pass them through there if needed
141
        $settings = ArrayHelper::remove($config, 'settings', []);
142
143
        if (is_string($settings)) {
144
            $settings = Json::decodeIfJson($settings);
145
        }
146
147
        if (!is_array($settings)) {
148
            $settings = [$settings];
149
        }
150
151
        return $settings;
152
    }
153
154
    /*******************************************
155
     * PREPARE
156
     *******************************************/
157
158
    /**
159
     * @inheritdoc
160
     *
161
     * @throws QueryAbortedException if it can be determined that there won’t be any results
162
     */
163
    public function prepare($builder)
164
    {
165
        $this->applyProviderConditions();
166
        $this->applyAuditAttributeConditions();
167
168
        return parent::prepare($builder);
169
    }
170
}
171