Completed
Push — develop ( bc4d8c...856b14 )
by Nate
30:34
created

ProviderQuery::populate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 10
cp 0
rs 9.8666
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 12
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\db;
10
11
use craft\db\Query;
12
use craft\db\QueryAbortedException;
13
use craft\helpers\ArrayHelper;
14
use flipbox\ember\db\traits\AuditAttributes;
15
use flipbox\ember\db\traits\FixedOrderBy;
16
use flipbox\patron\helpers\ProviderHelper;
17
use flipbox\patron\Patron;
18
use flipbox\patron\records\Provider;
19
20
/**
21
 * @author Flipbox Factory <[email protected]>
22
 * @since 1.0.0
23
 */
24
class ProviderQuery extends Query
25
{
26
    use traits\ProviderAttributes,
27
        FixedOrderBy,
28
        AuditAttributes;
29
30
    /**
31
     * @inheritdoc
32
     */
33
    public function __construct($config = [])
34
    {
35
        $this->orderBy = [Provider::tableAlias() . '.dateCreated' => SORT_DESC];
36
        $this->from = [Provider::tableName() . ' ' . Provider::tableAlias()];
37
        $this->select = [Provider::tableAlias() . '.*'];
38
39
        parent::__construct($config);
40
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45
    public function init()
46
    {
47
        parent::init();
48
49
        if ($this->environment === null) {
50
            $this->environment = Patron::getInstance()->getSettings()->getEnvironment();
51
        }
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57
    public function populate($rows)
58
    {
59
        $results = parent::populate($rows);
60
61
        if (Patron::getInstance()->getSettings()->encryptStorageData === true) {
62
            foreach ($results as $key => $result) {
63
                $results[$key] = $this->parseResult($result, false);
64
            }
65
        }
66
67
        return $results;
68
    }
69
70
    /**
71
     * Decrypt
72
     * @inheritdoc
73
     */
74
    public function one($db = null)
75
    {
76
        return $this->parseResult(parent::one($db));
77
    }
78
79
    /**
80
     * @param $result
81
     * @return mixed
82
     * @throws \yii\base\Exception
83
     * @throws \yii\base\InvalidConfigException
84
     */
85
    protected function parseResult($result, bool $checkSettings = true)
86
    {
87
        if (!is_array($result)) {
88
            return $result;
89
        }
90
91
        // Extract 'clientSecret'
92
        $clientSecret = ArrayHelper::remove($result, 'clientSecret');
93
94
        if (!empty($clientSecret)) {
95
            $result['clientSecret'] = ProviderHelper::decryptClientSecret($clientSecret, $checkSettings);
96
        }
97
98
        return $result;
99
    }
100
101
102
    /*******************************************
103
     * FIXED ORDER
104
     *******************************************/
105
106
    /**
107
     * @inheritdoc
108
     */
109
    protected function fixedOrderColumn(): string
110
    {
111
        return 'id';
112
    }
113
114
115
    /*******************************************
116
     * PREPARE
117
     *******************************************/
118
119
    /**
120
     * @inheritdoc
121
     *
122
     * @throws QueryAbortedException if it can be determined that there won’t be any results
123
     */
124
    public function prepare($builder)
125
    {
126
        $this->applyConditions();
127
        $this->applyAuditAttributeConditions();
128
        $this->applyOrderByParams($builder->db);
129
130
        return parent::prepare($builder);
131
    }
132
}
133