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 rules() |
45
|
|
|
{ |
46
|
|
|
return array_merge( |
47
|
|
|
parent::rules(), |
48
|
|
|
$this->handleRules(), |
49
|
|
|
[ |
50
|
|
|
[ |
51
|
|
|
[ |
52
|
|
|
'class' |
53
|
|
|
], |
54
|
|
|
'required' |
55
|
|
|
], |
56
|
|
|
[ |
57
|
|
|
[ |
58
|
|
|
'handle' |
59
|
|
|
], |
60
|
|
|
UniqueValidator::class |
61
|
|
|
], |
62
|
|
|
[ |
63
|
|
|
[ |
64
|
|
|
'class', |
65
|
|
|
'settings' |
66
|
|
|
], |
67
|
|
|
'safe', |
68
|
|
|
'on' => [ |
69
|
|
|
ModelHelper::SCENARIO_DEFAULT |
70
|
|
|
] |
71
|
|
|
] |
72
|
|
|
] |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param static $record |
78
|
|
|
* @param $row |
79
|
|
|
*/ |
80
|
|
|
public static function populateRecord($record, $row) |
81
|
|
|
{ |
82
|
|
|
parent::populateRecord($record, $row); |
83
|
|
|
|
84
|
|
|
$settings = $record->settings; |
85
|
|
|
|
86
|
|
|
if (is_string($settings)) { |
87
|
|
|
$settings = Json::decodeIfJson($settings); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$record->setOldAttribute('settings', $settings); |
91
|
|
|
$record->setAttribute('settings', $settings); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return ConnectionConfigurationInterface |
96
|
|
|
*/ |
97
|
|
|
public function getConfiguration(): ConnectionConfigurationInterface |
98
|
|
|
{ |
99
|
|
|
if ($this->type === null) { |
100
|
|
|
if (null === ($type = $this->getConnectionManager()->findConfiguration($this))) { |
101
|
|
|
$type = new DefaultConfiguration($this); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$this->type = $type; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $this->type; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|