1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Components Bundle Project |
5
|
|
|
* |
6
|
|
|
* (c) Daniel West <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentsBundle\EventListener\Api; |
15
|
|
|
|
16
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
17
|
|
|
use Silverback\ApiComponentsBundle\Entity\Core\Route; |
18
|
|
|
use Silverback\ApiComponentsBundle\Exception\InvalidArgumentException; |
19
|
|
|
use Silverback\ApiComponentsBundle\Helper\Route\RouteGeneratorInterface; |
20
|
|
|
use Symfony\Component\HttpFoundation\Request; |
21
|
|
|
use Symfony\Component\HttpKernel\Event\ViewEvent; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @author Daniel West <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class RouteEventListener |
27
|
|
|
{ |
28
|
|
|
private RouteGeneratorInterface $routeGenerator; |
29
|
|
|
private ManagerRegistry $registry; |
30
|
|
|
|
31
|
|
|
public function __construct(RouteGeneratorInterface $routeGenerator, ManagerRegistry $registry) |
32
|
|
|
{ |
33
|
|
|
$this->routeGenerator = $routeGenerator; |
34
|
|
|
$this->registry = $registry; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function onPostValidate(ViewEvent $event): void |
38
|
|
|
{ |
39
|
|
|
$request = $event->getRequest(); |
40
|
|
|
$data = $request->attributes->get('data'); |
41
|
|
|
$operationName = $request->attributes->get('_api_collection_operation_name'); |
42
|
|
|
if ( |
43
|
|
|
empty($data) || |
44
|
|
|
!$data instanceof Route || |
45
|
|
|
'generate' !== $operationName |
46
|
|
|
) { |
47
|
|
|
return; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$this->generateRoute($data, $request); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function onPostWrite(ViewEvent $event): void |
54
|
|
|
{ |
55
|
|
|
$request = $event->getRequest(); |
56
|
|
|
$data = $request->attributes->get('data'); |
57
|
|
|
$operationName = $request->attributes->get('_api_item_operation_name'); |
58
|
|
|
if ( |
59
|
|
|
empty($data) || |
60
|
|
|
!$data instanceof Route || |
61
|
|
|
!\in_array($operationName, ['put', 'patch'], true) |
62
|
|
|
) { |
63
|
|
|
return; |
64
|
|
|
} |
65
|
|
|
$entityManager = $this->registry->getManagerForClass($className = Route::class); |
66
|
|
|
if (!$entityManager) { |
67
|
|
|
throw new InvalidArgumentException(sprintf('Could not find entity manager for %s', $className)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$previousRouteData = $request->attributes->get('previous_data'); |
71
|
|
|
$previousPath = $previousRouteData->getPath(); |
72
|
|
|
$newRedirect = $this->routeGenerator->createRedirect($previousPath, $data); |
73
|
|
|
$entityManager->persist($newRedirect); |
74
|
|
|
$entityManager->flush(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
private function generateRoute(Route $data, Request $request): void |
78
|
|
|
{ |
79
|
|
|
$page = $data->getPageData() ?? $data->getPage(); |
80
|
|
|
if (!$page) { |
81
|
|
|
throw new \LogicException('Validation should have already checked if the pageData or page values are set.'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$route = $this->routeGenerator->create($page, $data); |
85
|
|
|
|
86
|
|
|
$request->attributes->set('data', $route); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|