-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnector.php
185 lines (158 loc) · 5.05 KB
/
Connector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
namespace PlentyConnector\Connector;
use Assert\Assertion;
use PlentyConnector\Connector\ServiceBus\CommandFactory\CommandFactoryInterface;
use PlentyConnector\Connector\ServiceBus\CommandType;
use PlentyConnector\Connector\ServiceBus\QueryFactory\QueryFactoryInterface;
use PlentyConnector\Connector\ServiceBus\QueryType;
use PlentyConnector\Connector\ServiceBus\ServiceBusInterface;
use PlentyConnector\Connector\TransferObject\TransferObjectInterface;
use PlentyConnector\Connector\ValueObject\Definition\Definition;
use PlentyConnector\Console\OutputHandler\OutputHandlerInterface;
use Psr\Log\LoggerInterface;
/**
* Class Connector.
*/
class Connector implements ConnectorInterface
{
/**
* @var null|Definition[]
*/
private $definitions = [];
/**
* @var ServiceBusInterface
*/
private $serviceBus;
/**
* @var QueryFactoryInterface
*/
private $queryFactory;
/**
* @var CommandFactoryInterface
*/
private $commandFactory;
/**
* @var OutputHandlerInterface
*/
private $outputHandler;
/**
* @var LoggerInterface
*/
private $logger;
/**
* Connector constructor.
*
* @param ServiceBusInterface $serviceBus
* @param QueryFactoryInterface $queryFactory
* @param CommandFactoryInterface $commandFactory
* @param OutputHandlerInterface $outputHandler
* @param LoggerInterface $logger
*/
public function __construct(
ServiceBusInterface $serviceBus,
QueryFactoryInterface $queryFactory,
CommandFactoryInterface $commandFactory,
OutputHandlerInterface $outputHandler,
LoggerInterface $logger
) {
$this->serviceBus = $serviceBus;
$this->queryFactory = $queryFactory;
$this->commandFactory = $commandFactory;
$this->outputHandler = $outputHandler;
$this->logger = $logger;
}
/**
* {@inheritdoc}
*/
public function addDefinition(Definition $definition)
{
if (!$definition->isActive()) {
return;
}
$this->definitions[] = $definition;
$this->sortDefinitions();
}
/**
* {@inheritdoc}
*/
public function handle($queryType, $objectType = null, $identifier = null)
{
Assertion::inArray($queryType, QueryType::getAllTypes());
Assertion::nullOrString($objectType);
if ($queryType === QueryType::ONE) {
Assertion::notNull($identifier);
Assertion::uuid($identifier);
}
$definitions = $this->getDefinitions($objectType);
if (null === $definitions) {
$definitions = [];
}
if (empty($definitions)) {
$this->logger->notice('No definitions found');
}
array_walk($definitions, function (Definition $definition) use ($queryType, $identifier) {
$this->handleDefinition($definition, $queryType, $identifier);
});
}
/**
* sort definitions by priority. Highest priority needs to be on top of the array
*/
private function sortDefinitions()
{
usort($this->definitions, function (Definition $definitionLeft, Definition $definitionRight) {
if ($definitionLeft->getPriority() === $definitionRight->getPriority()) {
return 0;
}
return ($definitionLeft->getPriority() > $definitionRight->getPriority()) ? -1 : 1;
});
}
/**
* @param null $objectType
*
* @return Definition[]
*/
private function getDefinitions($objectType = null)
{
if (null === count($this->definitions)) {
return [];
}
$definitions = array_filter($this->definitions, function (Definition $definition) use ($objectType) {
return strtolower($definition->getObjectType()) === strtolower($objectType) || null === $objectType;
});
return $definitions;
}
/**
* @param Definition $definition
* @param int $queryType
* @param null|string $identifier
*/
private function handleDefinition(Definition $definition, $queryType, $identifier = null)
{
$this->outputHandler->writeLine(sprintf(
'handling definition: Type: %s, %s -> %s',
$definition->getObjectType(),
$definition->getOriginAdapterName(),
$definition->getDestinationAdapterName()
));
/**
* @var TransferObjectInterface[] $objects
*/
$objects = $this->serviceBus->handle($this->queryFactory->create(
$definition->getOriginAdapterName(),
$definition->getObjectType(),
$queryType,
$identifier
));
if (empty($objects)) {
$objects = [];
}
foreach ($objects as $object) {
$this->serviceBus->handle($this->commandFactory->create(
$definition->getDestinationAdapterName(),
$object->getType(),
CommandType::HANDLE,
$object
));
}
}
}