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