-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathNemDelingEventsController.php
More file actions
70 lines (59 loc) · 2.19 KB
/
Copy pathNemDelingEventsController.php
File metadata and controls
70 lines (59 loc) · 2.19 KB
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
<?php
declare(strict_types=1);
namespace App\NemDeling\Controller;
use App\NemDeling\Mapper\EventDataMapper;
use App\NemDeling\Model\NemDelingResult;
use App\NemDeling\NemDelingConfig;
use App\NemDeling\NemDelingService;
use App\NemDeling\NemDelingSyncLock;
use App\NemDeling\Xml\NemDelingXmlParser;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\Routing\Attribute\Route;
#[AsController]
final class NemDelingEventsController
{
public function __construct(
private readonly NemDelingXmlParser $xmlParser,
private readonly EventDataMapper $eventDataMapper,
private readonly NemDelingService $nemDelingService,
private readonly NemDelingSyncLock $syncLock,
private readonly NemDelingConfig $config,
private readonly LoggerInterface $logger,
) {}
#[Route('/api/v1/nemdeling/events', name: 'nemdeling_events', methods: ['POST'])]
public function __invoke(Request $request): Response
{
$this->logger->debug('NemDeling events webhook received.');
$this->syncLock->acquireEvents();
try {
$body = $this->xmlParser->parse($request->getContent());
$mapped = $this->eventDataMapper->map(
$body,
$this->config->getEventTemplateTitle(),
'event'
);
$results = $this->nemDelingService->syncEvents($mapped);
return $this->createResponse($results);
} finally {
$this->syncLock->releaseEvents();
}
}
/**
* @param NemDelingResult[] $results
*/
private function createResponse(array $results): Response
{
$payload = array_map(
static fn (NemDelingResult $result): array => [
'name' => $result->name,
'status' => $result->status,
],
$results
);
$this->logger->info('NemDeling events result: '.json_encode($payload, JSON_THROW_ON_ERROR));
return new Response('OK: '.json_encode($payload, JSON_THROW_ON_ERROR), Response::HTTP_CREATED);
}
}