IntegrationConnection::populateRecord()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
ccs 0
cts 5
cp 0
rs 10
cc 1
nc 1
nop 2
crap 2
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\records;
10
11
use Craft;
12
use craft\helpers\Json;
13
use flipbox\craft\ember\models\HandleRulesTrait;
14
use flipbox\craft\ember\records\ActiveRecordWithId;
15
use flipbox\craft\integration\queries\IntegrationConnectionQuery;
16
use yii\validators\UniqueValidator;
17
18
/**
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 2.0.0
21
 *
22
 * @property string $name
23
 * @property string $class
24
 * @property array $settings
25
 */
26
abstract class IntegrationConnection extends ActiveRecordWithId
27
{
28
    use HandleRulesTrait;
29
30
    /**
31
     * The query class this record uses
32
     */
33
    const QUERY_CLASS = IntegrationConnectionQuery::class;
34
35
    /**
36
     * @param static $record
37
     * @param $row
38
     */
39
    public static function populateRecord($record, $row)
40
    {
41
        parent::populateRecord($record, $row);
42
        $record->setOldAttribute('settings', $record->ensureSettings());
43
    }
44
45
46
    /*******************************************
47
     * QUERY
48
     *******************************************/
49
50
    /**
51
     * @noinspection PhpDocMissingThrowsInspection
52
     * @return IntegrationConnectionQuery
53
     */
54
    public static function find(): IntegrationConnectionQuery
55
    {
56
        /** @noinspection PhpIncompatibleReturnTypeInspection */
57
        /** @noinspection PhpUnhandledExceptionInspection */
58
        return Craft::createObject(
59
            static::QUERY_CLASS,
60
            [
61
                get_called_class()
62
            ]
63
        );
64
    }
65
66
    /**
67
     * @inheritdoc
68
     */
69
    protected static function findByCondition($condition)
70
    {
71
        if (!is_numeric($condition) && is_string($condition)) {
72
            $condition = ['handle' => $condition];
73
        }
74
75
        /** @noinspection PhpInternalEntityUsedInspection */
76
        return parent::findByCondition($condition);
77
    }
78
79
80
    /*******************************************
81
     * RULES
82
     *******************************************/
83
84
    /**
85
     * @inheritdoc
86
     */
87
    public function rules()
88
    {
89
        return array_merge(
90
            parent::rules(),
91
            $this->handleRules(),
92
            [
93
                [
94
                    [
95
                        'class',
96
                        'name'
97
                    ],
98
                    'required'
99
                ],
100
                [
101
                    [
102
                        'handle'
103
                    ],
104
                    UniqueValidator::class
105
                ],
106
                [
107
                    [
108
                        'name',
109
                        'class',
110
                        'settings',
111
                        'enabled'
112
                    ],
113
                    'safe',
114
                    'on' => [
115
                        static::SCENARIO_DEFAULT
116
                    ]
117
                ]
118
            ]
119
        );
120
    }
121
122
123
    /*******************************************
124
     * EVENTS
125
     *******************************************/
126
127
    /**
128
     * @param bool $insert
129
     * @param array $changedAttributes
130
     */
131
    public function afterSave($insert, $changedAttributes)
132
    {
133
        parent::afterSave($insert, $changedAttributes);
134
        $this->ensureSettings();
135
    }
136
137
138
    /*******************************************
139
     * SETTINGS
140
     *******************************************/
141
142
    /**
143
     * @param string $attribute
144
     * @return mixed
145
     */
146
    public function getSettingsValue(string $attribute)
147
    {
148
        $settings = $this->ensureSettings();
149
        return $settings[$attribute] ?? null;
150
    }
151
152
    /**
153
     * @param string $attribute
154
     * @param $value
155
     * @return $this
156
     */
157
    public function setSettingsValue(string $attribute, $value)
158
    {
159
        $settings = $this->ensureSettings();
160
        $settings[$attribute] = $value;
161
162
        $this->setAttribute('settings', $settings);
163
        return $this;
164
    }
165
166
    /**
167
     * @return array|null
168
     */
169
    protected function ensureSettings()
170
    {
171
        $settings = $this->getAttribute('settings');
172
173
        if (is_string($settings)) {
174
            $settings = Json::decodeIfJson($settings);
175
        }
176
177
        $this->setAttribute('settings', $settings);
178
179
        return $settings;
180
    }
181
}
182