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\services; |
10
|
|
|
|
11
|
|
|
use flipbox\craft\integration\connections\ConnectionConfigurationInterface; |
12
|
|
|
use flipbox\craft\integration\events\RegisterConnectionConfigurationsEvent; |
13
|
|
|
use flipbox\craft\integration\records\IntegrationConnection as Connection; |
14
|
|
|
use flipbox\ember\exceptions\ObjectNotFoundException; |
15
|
|
|
use flipbox\ember\services\traits\records\AccessorByString; |
16
|
|
|
use yii\base\Component; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Flipbox Factory <[email protected]> |
20
|
|
|
* @since 1.1.0 |
21
|
|
|
* |
22
|
|
|
* @method Connection create(array $attributes = []) |
23
|
|
|
* @method Connection find($identifier) |
24
|
|
|
* @method Connection get($identifier) |
25
|
|
|
* @method Connection findByString($identifier) |
26
|
|
|
* @method Connection getByString($identifier) |
27
|
|
|
* @method Connection findByCondition($condition = []) |
28
|
|
|
* @method Connection getByCondition($condition = []) |
29
|
|
|
* @method Connection findByCriteria($criteria = []) |
30
|
|
|
* @method Connection getByCriteria($criteria = []) |
31
|
|
|
* @method Connection [] findAllByCondition($condition = []) |
32
|
|
|
* @method Connection [] getAllByCondition($condition = []) |
33
|
|
|
* @method Connection [] findAllByCriteria($criteria = []) |
34
|
|
|
* @method Connection [] getAllByCriteria($criteria = []) |
35
|
|
|
*/ |
36
|
|
|
abstract class IntegrationConnectionManager extends Component |
37
|
|
|
{ |
38
|
|
|
use AccessorByString; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @event RegisterConnectionsEvent The event that is triggered when registering connections. |
42
|
|
|
*/ |
43
|
|
|
const EVENT_REGISTER_CONFIGURATIONS = 'registerConfigurations'; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritdoc |
47
|
|
|
*/ |
48
|
|
|
protected function stringProperty(): string |
49
|
|
|
{ |
50
|
|
|
return 'handle'; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param Connection $connection |
55
|
|
|
* @return ConnectionConfigurationInterface[] |
56
|
|
|
*/ |
57
|
|
|
public function getConfigurations(Connection $connection): array |
58
|
|
|
{ |
59
|
|
|
$event = new RegisterConnectionConfigurationsEvent; |
60
|
|
|
|
61
|
|
|
$this->trigger(self::EVENT_REGISTER_CONFIGURATIONS, $event); |
62
|
|
|
|
63
|
|
|
$configurations = []; |
64
|
|
|
foreach ($event->configurations as $class => $configuration) { |
65
|
|
|
$configurations[$class] = new $configuration($connection); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $configurations; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param Connection $connection |
73
|
|
|
* @return ConnectionConfigurationInterface|null |
74
|
|
|
*/ |
75
|
|
|
public function findConfiguration(Connection $connection) |
76
|
|
|
{ |
77
|
|
|
$class = $connection->class ?: null; |
78
|
|
|
|
79
|
|
|
if ($class === null) { |
80
|
|
|
return null; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$types = $this->getConfigurations($connection); |
84
|
|
|
return $types[$class] ?? null; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param Connection $connection |
89
|
|
|
* @return ConnectionConfigurationInterface |
90
|
|
|
* @throws ObjectNotFoundException |
91
|
|
|
*/ |
92
|
|
|
public function getConfiguration(Connection $connection): ConnectionConfigurationInterface |
93
|
|
|
{ |
94
|
|
|
if (null === ($type = $this->findConfiguration($connection))) { |
95
|
|
|
throw new ObjectNotFoundException("Unable to find connection type"); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $type; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|