-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProcessingMiddleware.php
87 lines (75 loc) · 3.52 KB
/
ProcessingMiddleware.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
<?php
namespace ESB\Middleware\Core;
use ESB\CoreHandlerInterface;
use ESB\DTO\ProcessingData;
use ESB\DTO\TargetRequest;
use ESB\Entity\Route;
use ESB\Exception\DuplicateRecordException;
use ESB\Exception\RouteConfigException;
use ESB\Middleware\ESBMiddlewareInterface;
use ESB\Repository\RouteRepositoryInterface;
use ESB\Repository\SyncRecordRepositoryInterface;
use ESB\Service\DynamicPropertiesFetcherInterface;
use ESB\Utils\Spaceless;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Source;
class ProcessingMiddleware implements ESBMiddlewareInterface
{
public function __construct(
private readonly SyncRecordRepositoryInterface $recordRepository,
private readonly Environment $twig,
private readonly RouteRepositoryInterface $routeRepository,
private readonly DynamicPropertiesFetcherInterface $dynamicPropertiesFetcher,
) {
}
public function process(ProcessingData $data, Route $route, CoreHandlerInterface $handler) : ProcessingData
{
// If exist sync settings, need check before sending data actuality of them
if ($settings = $route->syncSettings()) {
$prevSyncedRecord = $this->recordRepository->findByPk(
$settings->table(),
$this->twig->createTemplate($settings->pkPath())->render($data->incomeData->jsonSerialize()) ?: throw new RouteConfigException('Invalid syncSettings::pkPath')
);
/** no update, due to settings, available - so exit */
if ($settings->syncOnExist() === false && $prevSyncedRecord) {
throw new DuplicateRecordException();
}
$data = $data->withSyncData($prevSyncedRecord);
if ($prevSyncedRecord && $updateRouteId = $settings->updateRouteId()) {
$route = $this->routeRepository->get($updateRouteId);
}
}
if (! $route->toSystemData()->template()) {
return $handler->handle(
$data->withTargetRequest(new TargetRequest('', ($this->dynamicPropertiesFetcher)($data->incomeData, $route->toSystemData()->headers()))),
$route,
);
}
/** We implement spaceless on template content to remove all special chars.
* Code within try block will work only with FilesystemLoader. By default, we expect template code passed from toSystemData
*/
try {
/** TODO is template nullable? */
$content = $this->twig->getLoader()->getSourceContext($route->toSystemData()->template() ?? '')->getCode();
$template = $this->twig->createTemplate((new Spaceless($content))());
} catch (LoaderError) {
/** TODO is template nullable? */
$this->twig->parse($this->twig->tokenize(new Source($route->toSystemData()->template() ?? '', '')));
$template = $this->twig->createTemplate(
(new Spaceless($route->toSystemData()->template() ?? ''))()
);
}
$content = $template->render($data->incomeData->jsonSerialize() + ['properties' => $route->fromSystemData()->properties]);
// nothing to update, same content passed, duplicate call
if ($content === $data->syncRecord?->requestBody()) {
throw new DuplicateRecordException();
}
return $handler->handle(
$data->withTargetRequest(
new TargetRequest($content, ($this->dynamicPropertiesFetcher)($data->incomeData, $route->toSystemData()->headers()))
),
$route
);
}
}