Completed
Push — master ( 3407e1...79fe03 )
by Nate
07:26
created

Connection::getConnection()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 0
cts 19
cp 0
rs 9.536
c 0
b 0
f 0
cc 4
nc 4
nop 0
crap 20
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/force/license
6
 * @link       https://www.flipboxfactory.com/software/force/
7
 */
8
9
namespace flipbox\craft\salesforce\records;
10
11
use craft\helpers\ArrayHelper;
12
use craft\helpers\Component as ComponentHelper;
13
use flipbox\craft\integration\records\IntegrationConnection;
14
use flipbox\craft\salesforce\connections\SavableConnectionInterface;
15
use flipbox\craft\salesforce\Force;
16
use flipbox\craft\salesforce\validators\ConnectionValidator;
17
use Flipbox\Salesforce\Connections\ConnectionInterface;
18
use Flipbox\Skeleton\Helpers\ObjectHelper;
19
20
/**
21
 * @author Flipbox Factory <[email protected]>
22
 * @since 1.0.0
23
 */
24
class Connection extends IntegrationConnection
25
{
26
    /**
27
     * The table name
28
     */
29
    const TABLE_ALIAS = 'salesforce_connections';
30
31
    /**
32
     * @var ConnectionInterface|null
33
     */
34
    private $connection;
35
36
    /**
37
     * @inheritdoc
38
     */
39
    public function rules()
40
    {
41
        return array_merge(
42
            parent::rules(),
43
            [
44
                [
45
                    [
46
                        'class'
47
                    ],
48
                    ConnectionValidator::class
49
                ]
50
            ]
51
        );
52
    }
53
54
    /**
55
     * @noinspection PhpDocMissingThrowsInspection
56
     * @return ConnectionInterface
57
     */
58
    public function getConnection(): ConnectionInterface
59
    {
60
        if (null === $this->connection) {
61
            $config = ComponentHelper::mergeSettings([
62
                'settings' => $this->settings,
63
                'class' => $this->class
64
            ]);
65
66
            // Apply overrides
67
            if (!empty($this->handle)) {
68
                if (null !== ($override = Force::getInstance()->getConnections()->getOverrides($this->handle))) {
69
                    $config = array_merge($config, $override);
70
                }
71
            }
72
73
            /** @noinspection PhpUnhandledExceptionInspection */
74
            $this->connection = ObjectHelper::create(
75
                $config,
76
                ConnectionInterface::class
77
            );
78
        }
79
80
        return $this->connection;
81
    }
82
83
84
    /*******************************************
85
     * EVENTS
86
     *******************************************/
87
88
    /**
89
     * @inheritdoc
90
     */
91
    public function beforeSave($insert)
92
    {
93
        $connection = $this->getConnection();
94
95
        if ($connection instanceof SavableConnectionInterface) {
96
            if (!$connection->beforeSave($insert)) {
97
                return false;
98
            }
99
        }
100
101
        return parent::beforeSave($insert);
102
    }
103
104
    /**
105
     * @inheritdoc
106
     */
107
    public function afterSave($insert, $changedAttributes)
108
    {
109
        $connection = $this->getConnection();
110
111
        if ($connection instanceof SavableConnectionInterface) {
112
            $connection->afterSave($insert, ArrayHelper::getValue($changedAttributes, 'settings'));
113
        }
114
115
        parent::afterSave($insert, $changedAttributes);
116
    }
117
118
    /**
119
     * @inheritdoc
120
     */
121
    public function beforeDelete()
122
    {
123
        $connection = $this->getConnection();
124
125
        if ($connection instanceof SavableConnectionInterface) {
126
            if (!$connection->beforeDelete()) {
127
                return false;
128
            }
129
        }
130
131
        return parent::beforeDelete();
132
    }
133
134
    /**
135
     * @inheritdoc
136
     */
137
    public function afterDelete()
138
    {
139
        $connection = $this->getConnection();
140
141
        if ($connection instanceof SavableConnectionInterface) {
142
            $connection->afterDelete();
143
        }
144
145
        parent::afterDelete();
146
    }
147
}
148