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\helpers\Json; |
12
|
|
|
use flipbox\craft\integration\connections\ConnectionConfigurationInterface; |
13
|
|
|
use flipbox\craft\integration\connections\DefaultConfiguration; |
14
|
|
|
use flipbox\craft\integration\services\IntegrationConnectionManager; |
15
|
|
|
use flipbox\ember\helpers\ModelHelper; |
16
|
|
|
use flipbox\ember\records\ActiveRecordWithId; |
17
|
|
|
use flipbox\ember\traits\HandleRules; |
18
|
|
|
use yii\validators\UniqueValidator; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Flipbox Factory <[email protected]> |
22
|
|
|
* @since 1.1.0 |
23
|
|
|
* |
24
|
|
|
* @property string $class |
25
|
|
|
* @property array $settings |
26
|
|
|
*/ |
27
|
|
|
abstract class IntegrationConnection extends ActiveRecordWithId |
28
|
|
|
{ |
29
|
|
|
use HandleRules; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ConnectionConfigurationInterface |
33
|
|
|
*/ |
34
|
|
|
private $type; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return IntegrationConnectionManager |
38
|
|
|
*/ |
39
|
|
|
abstract protected function getConnectionManager(): IntegrationConnectionManager; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritdoc |
43
|
|
|
*/ |
44
|
|
|
public function init() |
45
|
|
|
{ |
46
|
|
|
parent::init(); |
47
|
|
|
|
48
|
|
|
// Normalize settings |
49
|
|
|
$this->setAttribute( |
50
|
|
|
'settings', |
51
|
|
|
static::normalizeSettings($this->getAttribute('settings')) |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @inheritdoc |
57
|
|
|
*/ |
58
|
|
|
public function rules() |
59
|
|
|
{ |
60
|
|
|
return array_merge( |
61
|
|
|
parent::rules(), |
62
|
|
|
$this->handleRules(), |
63
|
|
|
[ |
64
|
|
|
[ |
65
|
|
|
[ |
66
|
|
|
'class' |
67
|
|
|
], |
68
|
|
|
'required' |
69
|
|
|
], |
70
|
|
|
[ |
71
|
|
|
[ |
72
|
|
|
'handle' |
73
|
|
|
], |
74
|
|
|
UniqueValidator::class |
75
|
|
|
], |
76
|
|
|
[ |
77
|
|
|
[ |
78
|
|
|
'class', |
79
|
|
|
'settings' |
80
|
|
|
], |
81
|
|
|
'safe', |
82
|
|
|
'on' => [ |
83
|
|
|
ModelHelper::SCENARIO_DEFAULT |
84
|
|
|
] |
85
|
|
|
] |
86
|
|
|
] |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param static $record |
92
|
|
|
* @param $row |
93
|
|
|
*/ |
94
|
|
|
public static function populateRecord($record, $row) |
95
|
|
|
{ |
96
|
|
|
parent::populateRecord($record, $row); |
97
|
|
|
|
98
|
|
|
$settings = static::normalizeSettings($record->settings); |
99
|
|
|
|
100
|
|
|
$record->setOldAttribute('settings', $settings); |
101
|
|
|
$record->setAttribute('settings', $settings); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param $settings |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
|
|
protected static function normalizeSettings($settings): array |
109
|
|
|
{ |
110
|
|
|
if (is_string($settings)) { |
111
|
|
|
$settings = Json::decodeIfJson($settings); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if (!is_array($settings)) { |
115
|
|
|
$settings = array_filter([$settings]); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $settings; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return ConnectionConfigurationInterface |
123
|
|
|
*/ |
124
|
|
|
public function getConfiguration(): ConnectionConfigurationInterface |
125
|
|
|
{ |
126
|
|
|
if ($this->type === null) { |
127
|
|
|
if (null === ($type = $this->getConnectionManager()->findConfiguration($this))) { |
128
|
|
|
$type = new DefaultConfiguration($this); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$this->type = $type; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $this->type; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|