Completed
Push — develop ( edb8bf...6f43a0 )
by Nate
08:26
created

ProviderQuery   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 10

Importance

Changes 0
Metric Value
wmc 14
lcom 2
cbo 10
dl 0
loc 141
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
     */
64
    public function populate($rows)
65
    {
66
        $results = parent::populate($rows);
67
68
        if (Patron::getInstance()->getSettings()->getEncryptStorageData() === true) {
69
            foreach ($results as $key => $result) {
70
                $results[$key] = $this->createObject($result, false);
71
            }
72
        }
73
74
        return $results;
75
    }
76
77
    /**
78
     * @inheritdoc
79
     * @return AbstractProvider
80
     * @throws \yii\base\InvalidConfigException
81
     */
82
    public function one($db = null)
83
    {
84
        if (null === ($config = parent::one($db))) {
85
            return null;
86
        }
87
88
        return $this->createObject($config);
89
    }
90
91
    /**
92
     * @param array $config
93
     * @return mixed
94
     * @throws \yii\base\InvalidConfigException
95
     */
96
    protected function createObject(array $config, bool $checkSettings = true)
97
    {
98
        $config = $this->prepareConfig($config, $checkSettings);
99
100
        // Provider class
101
        $class = ObjectHelper::checkConfig(
102
            $config,
103
            AbstractProvider::class
104
        );
105
106
        return new $class($config);
107
    }
108
109
    /**
110
     * @param array $config
111
     * @return array
112
     */
113
    protected function prepareConfig(array $config = [], bool $checkSettings = true): array
114
    {
115
        // Extract 'clientSecret'
116
        $clientSecret = ArrayHelper::remove($config, 'clientSecret');
117
118
        if (!empty($clientSecret)) {
119
            $config['clientSecret'] = ProviderHelper::decryptClientSecret($clientSecret, $checkSettings);
120
        }
121
122
        // Merge in settings
123
        $config = array_merge($config, $this->extractSettings($config));
124
125
        // This doesn't change
126
        $config['redirectUri'] = Patron::getInstance()->getSettings()->getCallbackUrl();
127
128
        return $config;
129
    }
130
131
    /**
132
     * @param array $config
133
     * @return array
134
     */
135
    protected function extractSettings(array &$config): array
136
    {
137
        // We could init the SettingsInterface and pass them through there if needed
138
        $settings = ArrayHelper::remove($config, 'settings', []);
139
140
        if (is_string($settings)) {
141
            $settings = Json::decodeIfJson($settings);
142
        }
143
144
        if (!is_array($settings)) {
145
            $settings = [$settings];
146
        }
147
148
        return $settings;
149
    }
150
151
    /*******************************************
152
     * PREPARE
153
     *******************************************/
154
155
    /**
156
     * @inheritdoc
157
     *
158
     * @throws QueryAbortedException if it can be determined that there won’t be any results
159
     */
160
    public function prepare($builder)
161
    {
162
        $this->applyProviderConditions();
163
        $this->applyAuditAttributeConditions();
164
165
        return parent::prepare($builder);
166
    }
167
}
168