Connection   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 6
dl 0
loc 126
ccs 0
cts 72
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A rules() 0 14 1
A getConnection() 0 24 4
A beforeSave() 0 12 3
A afterSave() 0 13 3
A beforeDelete() 0 12 3
A afterDelete() 0 10 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(
112
                $insert,
113
                (array)ArrayHelper::getValue($changedAttributes, 'settings', []) ?: []
114
            );
115
        }
116
117
        parent::afterSave($insert, $changedAttributes);
118
    }
119
120
    /**
121
     * @inheritdoc
122
     */
123
    public function beforeDelete()
124
    {
125
        $connection = $this->getConnection();
126
127
        if ($connection instanceof SavableConnectionInterface) {
128
            if (!$connection->beforeDelete()) {
129
                return false;
130
            }
131
        }
132
133
        return parent::beforeDelete();
134
    }
135
136
    /**
137
     * @inheritdoc
138
     */
139
    public function afterDelete()
140
    {
141
        $connection = $this->getConnection();
142
143
        if ($connection instanceof SavableConnectionInterface) {
144
            $connection->afterDelete();
145
        }
146
147
        parent::afterDelete();
148
    }
149
}
150