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

ManageProviders::defaultEnvironments()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 7
cp 0
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
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 flipbox\ember\services\traits\records\AccessorByString;
13
use flipbox\patron\db\ProviderQuery;
14
use flipbox\patron\Patron;
15
use flipbox\patron\records\Provider;
16
use yii\base\Component;
17
use yii\db\QueryInterface;
18
19
/**
20
 * @author Flipbox Factory <[email protected]>
21
 * @since 1.0.0
22
 *
23
 * @method ProviderQuery getQuery($config = [])
24
 * @method Provider create(array $attributes = [])
25
 * @method Provider find($identifier)
26
 * @method Provider get($identifier)
27
 * @method Provider findByString($identifier)
28
 * @method Provider getByString($identifier)
29
 * @method Provider findByCondition($condition = [])
30
 * @method Provider getByCondition($condition = [])
31
 * @method Provider findByCriteria($criteria = [])
32
 * @method Provider getByCriteria($criteria = [])
33
 * @method Provider[] findAll()
34
 * @method Provider[] findAllByCondition($condition = [])
35
 * @method Provider[] getAllByCondition($condition = [])
36
 * @method Provider[] findAllByCriteria($criteria = [])
37
 * @method Provider[] getAllByCriteria($criteria = [])
38
 */
39
class ManageProviders extends Component
40
{
41
    use AccessorByString {
42
        buildQueryFromCondition as parentBuildQueryFromCondition;
43
    }
44
45
    /**
46
     * @param array $condition
47
     * @return QueryInterface
48
     */
49
    protected function buildQueryFromCondition($condition = []): QueryInterface
50
    {
51
        if (is_numeric($condition)) {
52
            $condition = ['id' => $condition];
53
        }
54
55
        return $this->parentBuildQueryFromCondition($condition);
56
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61
    public static function recordClass(): string
62
    {
63
        return Provider::class;
64
    }
65
66
    /**
67
     * @inheritdoc
68
     */
69
    public static function stringProperty(): string
70
    {
71
        return 'handle';
72
    }
73
74
    /**
75
     * @param array $config
76
     * @return array
77
     */
78
    protected function prepareQueryConfig($config = [])
79
    {
80
        if (!is_array($config)) {
81
            $config = ArrayHelper::toArray($config, [], true);
82
        }
83
84
        // Allow disabled when managing
85
        if (!array_key_exists('enabled', $config)) {
86
            $config['enabled'] = null;
87
        }
88
89
        // Allow all environments when managing
90
        if (!array_key_exists('environment', $config)) {
91
            $config['environment'] = null;
92
        }
93
94
        return $config;
95
    }
96
97
    /*******************************************
98
     * ENCRYPTION
99
     *******************************************/
100
101
    /**
102
     * @param bool $changeTo
103
     * @return void
104
     */
105
    public function changeEncryption(bool $changeTo)
106
    {
107
        // Temp
108
        Patron::getInstance()->getSettings()->setEncryptStorageData(!$changeTo);
109
110
        // Get current providers
111
        $records = $this->findAll();
112
113
        // Temp
114
        Patron::getInstance()->getSettings()->setEncryptStorageData($changeTo);
115
116
        // Iterate and save
117
        foreach ($records as $record) {
118
            Patron::info(
119
                'Altering Provider::$clientSecret encryption preferences'
120
            );
121
122
            $record->save();
123
        }
124
    }
125
126
    /*******************************************
127
     * STATES
128
     *******************************************/
129
130
    /**
131
     * @param Provider $record
132
     * @return bool
133
     */
134
    public function disable(Provider $record)
135
    {
136
        $record->enabled = false;
137
        return $record->save(true, ['enabled']);
138
    }
139
140
    /**
141
     * @param Provider $record
142
     * @return bool
143
     */
144
    public function enable(Provider $record)
145
    {
146
        $record->enabled = true;
147
        return $record->save(true, ['enabled']);
148
    }
149
}
150