Completed
Push — develop ( 2d4f17...d5975b )
by Nate
05:25
created

Connection::rules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 14
cp 0
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/hubspot/license
6
 * @link       https://www.flipboxfactory.com/software/hubspot/
7
 */
8
9
namespace flipbox\craft\hubspot\records;
10
11
use craft\helpers\ArrayHelper;
12
use craft\helpers\Component as ComponentHelper;
13
use flipbox\craft\hubspot\connections\SavableConnectionInterface;
14
use flipbox\craft\hubspot\HubSpot;
15
use flipbox\craft\hubspot\validators\ConnectionValidator;
16
use flipbox\craft\integration\records\IntegrationConnection;
17
use Flipbox\HubSpot\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 = 'hubspot_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 = HubSpot::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
     * EVENTS
85
     *******************************************/
86
87
    /**
88
     * @inheritdoc
89
     */
90
    public function beforeSave($insert)
91
    {
92
        $connection = $this->getConnection();
93
94
        if ($connection instanceof SavableConnectionInterface) {
95
            if (!$connection->beforeSave($insert)) {
96
                return false;
97
            }
98
        }
99
100
        return parent::beforeSave($insert);
101
    }
102
103
    /**
104
     * @inheritdoc
105
     */
106
    public function afterSave($insert, $changedAttributes)
107
    {
108
        $connection = $this->getConnection();
109
110
        if ($connection instanceof SavableConnectionInterface) {
111
            $connection->afterSave($insert, ArrayHelper::getValue($changedAttributes, 'settings'));
112
        }
113
114
        parent::afterSave($insert, $changedAttributes);
115
    }
116
117
    /**
118
     * @inheritdoc
119
     */
120
    public function beforeDelete()
121
    {
122
        $connection = $this->getConnection();
123
124
        if ($connection instanceof SavableConnectionInterface) {
125
            if (!$connection->beforeDelete()) {
126
                return false;
127
            }
128
        }
129
130
        return parent::beforeDelete();
131
    }
132
133
    /**
134
     * @inheritdoc
135
     */
136
    public function afterDelete()
137
    {
138
        $connection = $this->getConnection();
139
140
        if ($connection instanceof SavableConnectionInterface) {
141
            $connection->afterDelete();
142
        }
143
144
        parent::afterDelete();
145
    }
146
}
147