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