Completed
Push — develop ( 0fe799...8d013c )
by Nate
02:38
created

Delete::deleteConnection()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 0
cts 16
cp 0
rs 9.6333
c 0
b 0
f 0
cc 3
nc 5
nop 1
crap 12
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-integration/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-integration/
7
 */
8
9
namespace flipbox\craft\integration\actions\connections\traits;
10
11
use Craft;
12
use flipbox\craft\integration\records\IntegrationConnection;
13
use yii\db\ActiveRecord;
14
15
/**
16
 * @author Flipbox Factory <[email protected]>
17
 * @since 1.1.1
18
 */
19
trait Delete
20
{
21
    /**
22
     * @inheritdoc
23
     * @throws \Exception
24
     * @throws \yii\db\Exception
25
     */
26
    protected function performAction(ActiveRecord $record): bool
27
    {
28
        if (!$record instanceof IntegrationConnection) {
29
            return false;
30
        }
31
32
        return $this->deleteConnection($record);
33
    }
34
35
    /**
36
     * @param IntegrationConnection $connection
37
     * @return bool
38
     * @throws \Exception
39
     * @throws \yii\db\Exception
40
     */
41
    protected function deleteConnection(IntegrationConnection $connection): bool
42
    {
43
        // Db transaction
44
        $transaction = Craft::$app->getDb()->beginTransaction();
45
46
        try {
47
            if (!$connection->getConfiguration()->delete()) {
48
                $connection->addError('configuration', 'Unable to delete configuration.');
49
                $transaction->rollBack();
50
                return false;
51
            }
52
        } catch (\Exception $e) {
53
            $transaction->rollBack();
54
            throw $e;
55
        }
56
57
        $transaction->commit();
58
        return true;
59
    }
60
}
61