ProviderQuery   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 9
dl 0
loc 129
ccs 0
cts 61
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 10 1
A one() 0 8 2
A createObject() 0 12 1
A prepareConfig() 0 18 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\craft\ember\queries\PopulateObjectTrait;
18
use flipbox\patron\Patron;
19
use flipbox\patron\records\Provider;
20
use League\OAuth2\Client\Provider\AbstractProvider;
21
22
/**
23
 * @author Flipbox Factory <[email protected]>
24
 * @since 1.0.0
25
 */
26
class ProviderQuery extends Query
27
{
28
    use ProviderAttributesTrait,
29
        AuditAttributesTrait,
30
        PopulateObjectTrait;
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
        ];
50
51
        parent::init();
52
    }
53
54
55
    /*******************************************
56
     * RESULTS
57
     *******************************************/
58
59
    /**
60
     * @inheritdoc
61
     * @return array|mixed|null
62
     * @throws \yii\base\InvalidConfigException
63
     */
64
    public function one($db = null)
65
    {
66
        if (null === ($config = parent::one($db))) {
67
            return null;
68
        }
69
70
        return $this->createObject($config);
71
    }
72
73
    /*******************************************
74
     * CREATE OBJECT
75
     *******************************************/
76
77
    /**
78
     * @param array $config
79
     * @return mixed
80
     * @throws \yii\base\InvalidConfigException
81
     */
82
    protected function createObject($config)
83
    {
84
        $config = $this->prepareConfig($config);
85
86
        // Provider class
87
        $class = ObjectHelper::checkConfig(
88
            $config,
89
            AbstractProvider::class
90
        );
91
92
        return new $class($config);
93
    }
94
95
    /**
96
     * @param array $config
97
     * @return array
98
     */
99
    protected function prepareConfig(array $config = []): array
100
    {
101
        // Merge in settings
102
        $config = array_merge($config, $this->extractSettings($config));
103
104
        // Apply override settings
105
        if (null !== ($handle = $config['handle'] ?? null)) {
106
            $config = array_merge(
107
                $config,
108
                Patron::getInstance()->getSettings()->getProvider($handle)
109
            );
110
        }
111
112
        // This doesn't change
113
        $config['redirectUri'] = Patron::getInstance()->getSettings()->getCallbackUrl();
114
115
        return $config;
116
    }
117
118
    /**
119
     * @param array $config
120
     * @return array
121
     */
122
    protected function extractSettings(array &$config): array
123
    {
124
        // We could init the SettingsInterface and pass them through there if needed
125
        $settings = ArrayHelper::remove($config, 'settings', []);
126
127
        if (is_string($settings)) {
128
            $settings = Json::decodeIfJson($settings);
129
        }
130
131
        if (!is_array($settings)) {
132
            $settings = [$settings];
133
        }
134
135
        return $settings;
136
    }
137
138
    /*******************************************
139
     * PREPARE
140
     *******************************************/
141
142
    /**
143
     * @inheritdoc
144
     *
145
     * @throws QueryAbortedException if it can be determined that there won’t be any results
146
     */
147
    public function prepare($builder)
148
    {
149
        $this->applyProviderConditions();
150
        $this->applyAuditAttributeConditions();
151
152
        return parent::prepare($builder);
153
    }
154
}
155