Completed
Push — master ( e45dbf...3a73d5 )
by Nate
05:43 queued 04:08
created

AlterEnvironments::deleteOldProviderEnvironments()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 13
cp 0
rs 9.7998
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/organization/license
6
 * @link       https://www.flipboxfactory.com/software/organization/
7
 */
8
9
namespace flipbox\patron\migrations;
10
11
use craft\db\Migration;
12
use flipbox\patron\Patron;
13
use flipbox\patron\records\ProviderEnvironment;
14
use flipbox\patron\records\TokenEnvironment;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.0.0
19
 */
20
class AlterEnvironments extends Migration
21
{
22
    /**
23
     * The state column name
24
     */
25
    const COLUMN_NAME = 'environment';
26
27
    /**
28
     * @inheritdoc
29
     * @throws \Throwable
30
     * @throws \yii\db\StaleObjectException
31
     */
32
    public function safeUp()
33
    {
34
        $environments = Patron::getInstance()->getSettings()->getEnvironments();
35
36
        $this->deleteOldEnvironments($environments);
37
38
        $type = $this->enum(
39
            self::COLUMN_NAME,
40
            $environments
41
        )->notNull();
42
43
        $this->alterColumn(
44
            ProviderEnvironment::tableName(),
45
            self::COLUMN_NAME,
46
            $type
47
        );
48
49
        $this->alterColumn(
50
            TokenEnvironment::tableName(),
51
            self::COLUMN_NAME,
52
            $type
53
        );
54
    }
55
56
    /**
57
     * @param array $environments
58
     * @throws \Throwable
59
     * @throws \yii\db\StaleObjectException
60
     */
61
    protected function deleteOldEnvironments(array $environments)
62
    {
63
        $this->deleteOldProviderEnvironments($environments);
64
        $this->deleteOldTokenEnvironments($environments);
65
    }
66
67
    /**
68
     * @param array $environments
69
     * @throws \Throwable
70
     * @throws \yii\db\StaleObjectException
71
     */
72
    private function deleteOldTokenEnvironments(array $environments)
73
    {
74
        $records = TokenEnvironment::find()
75
            ->andWhere([
76
                'NOT IN',
77
                self::COLUMN_NAME,
78
                $environments
79
            ])
80
            ->all();
81
82
        foreach ($records as $record) {
83
            $record->delete();
84
        }
85
    }
86
87
    /**
88
     * @param array $environments
89
     * @throws \Throwable
90
     * @throws \yii\db\StaleObjectException
91
     */
92
    private function deleteOldProviderEnvironments(array $environments)
93
    {
94
        $records = ProviderEnvironment::find()
95
            ->andWhere([
96
                'NOT IN',
97
                self::COLUMN_NAME,
98
                $environments
99
            ])
100
            ->all();
101
102
        foreach ($records as $record) {
103
            $record->delete();
104
        }
105
    }
106
107
    /**
108
     * @inheritdoc
109
     */
110
    public function safeDown()
111
    {
112
        return false;
113
    }
114
}
115