|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Umbrellio\TableSync\Integration\Laravel\Receive\MessageData; |
|
6
|
|
|
|
|
7
|
|
|
use Umbrellio\TableSync\Integration\Laravel\Exceptions\Receive\IncorrectAdditionalDataHandler; |
|
8
|
|
|
use Umbrellio\TableSync\Integration\Laravel\Exceptions\Receive\IncorrectConfiguration; |
|
9
|
|
|
use Umbrellio\TableSync\Messages\ReceivedMessage; |
|
10
|
|
|
|
|
11
|
|
|
class MessageDataRetriever |
|
12
|
|
|
{ |
|
13
|
|
|
private $config; |
|
14
|
|
|
|
|
15
|
|
|
// todo: config should be object |
|
16
|
6 |
|
public function __construct(array $config) |
|
17
|
|
|
{ |
|
18
|
6 |
|
$this->config = $config; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
6 |
|
public function retrieve(ReceivedMessage $message): MessageData |
|
22
|
|
|
{ |
|
23
|
6 |
|
$messageConfig = $this->configForMessage($message); |
|
24
|
5 |
|
$table = $this->retrieveTable($messageConfig); |
|
25
|
4 |
|
$targetKeys = $this->retrieveTargetKeys($messageConfig); |
|
26
|
3 |
|
$data = $this->retrieveData($message, $messageConfig); |
|
27
|
|
|
|
|
28
|
3 |
|
return new MessageData($table, $targetKeys, $data); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
6 |
|
private function configForMessage(ReceivedMessage $message): array |
|
32
|
|
|
{ |
|
33
|
6 |
|
if (!isset($this->config[$message->getModel()])) { |
|
34
|
1 |
|
throw new IncorrectConfiguration("No configuration for {$message->getModel()}"); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
5 |
|
return $this->config[$message->getModel()]; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
5 |
|
private function retrieveTable(array $messageConfig): string |
|
41
|
|
|
{ |
|
42
|
5 |
|
if (!isset($messageConfig['table'])) { |
|
43
|
1 |
|
throw new IncorrectConfiguration('Table configuration required'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
4 |
|
return $messageConfig['table']; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
4 |
|
private function retrieveTargetKeys(array $messageConfig): array |
|
50
|
|
|
{ |
|
51
|
4 |
|
if (!isset($messageConfig['target_keys'])) { |
|
52
|
1 |
|
throw new IncorrectConfiguration('Target keys configuration required'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
3 |
|
return $messageConfig['target_keys']; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
3 |
|
private function retrieveData(ReceivedMessage $message, array $messageConfig): array |
|
59
|
|
|
{ |
|
60
|
3 |
|
$data = $this->wrapAttributes($message->getAttributes()); |
|
61
|
|
|
|
|
62
|
3 |
|
if (isset($messageConfig['additional_data_handler'])) { |
|
63
|
2 |
|
$data = $this->applyAdditionalHandler($data, $message, $messageConfig['additional_data_handler']); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
3 |
|
if (isset($messageConfig['override_data'])) { |
|
67
|
1 |
|
$data = $this->overrideData($data, $messageConfig['override_data']); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
3 |
|
return $data; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
3 |
|
private function wrapAttributes(array $attributes): array |
|
74
|
|
|
{ |
|
75
|
3 |
|
$keys = array_keys($attributes); |
|
76
|
|
|
|
|
77
|
3 |
|
$isArray = collect($keys) |
|
78
|
3 |
|
->every(function ($key) { |
|
79
|
3 |
|
return is_numeric($key); |
|
80
|
3 |
|
}); |
|
81
|
|
|
|
|
82
|
3 |
|
return $isArray ? $attributes : [$attributes]; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
2 |
|
private function applyAdditionalHandler( |
|
86
|
|
|
array $data, |
|
87
|
|
|
ReceivedMessage $message, |
|
88
|
|
|
string $additionalDataHandlerClass |
|
89
|
|
|
): array { |
|
90
|
2 |
|
$handler = $this->resolveAdditionalDataHandler($additionalDataHandlerClass); |
|
91
|
|
|
|
|
92
|
2 |
|
return array_map(function (array $item) use ($handler, $message) { |
|
93
|
2 |
|
return $handler->handle($message, $item); |
|
94
|
2 |
|
}, $data); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
1 |
|
private function overrideData(array $data, array $override): array |
|
98
|
|
|
{ |
|
99
|
1 |
|
return array_map(function (array $item) use ($override) { |
|
100
|
1 |
|
foreach ($override as $from => $to) { |
|
101
|
1 |
|
$item[$to] = $item[$from]; |
|
102
|
1 |
|
unset($item[$from]); |
|
103
|
|
|
} |
|
104
|
1 |
|
return $item; |
|
105
|
1 |
|
}, $data); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
2 |
|
private function resolveAdditionalDataHandler(string $className): AdditionalDataHandler |
|
109
|
|
|
{ |
|
110
|
2 |
|
$handler = app($className); |
|
111
|
2 |
|
if (!$handler instanceof AdditionalDataHandler) { |
|
112
|
|
|
throw new IncorrectAdditionalDataHandler( |
|
113
|
|
|
"Additional data handler must implement AdditionalDataHandler interface ({$className})" |
|
114
|
|
|
); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
2 |
|
return $handler; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|