1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Silverback\ApiComponentBundle\Factory\Entity\Route; |
4
|
|
|
|
5
|
|
|
use Cocur\Slugify\SlugifyInterface; |
6
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
7
|
|
|
use Silverback\ApiComponentBundle\Entity\Route\Route; |
8
|
|
|
use Silverback\ApiComponentBundle\Entity\Route\RouteAwareInterface; |
9
|
|
|
use Silverback\ApiComponentBundle\Factory\Entity\AbstractFactory; |
10
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
11
|
|
|
|
12
|
|
|
class RouteFactory extends AbstractFactory |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var SlugifyInterface |
16
|
|
|
*/ |
17
|
|
|
private $slugify; |
18
|
|
|
|
19
|
7 |
|
public function __construct( |
20
|
|
|
ObjectManager $manager, |
21
|
|
|
ValidatorInterface $validator, |
22
|
|
|
SlugifyInterface $slugify |
23
|
|
|
) |
24
|
|
|
{ |
25
|
7 |
|
$this->slugify = $slugify; |
26
|
7 |
|
parent::__construct($manager, $validator); |
27
|
7 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @inheritdoc |
31
|
|
|
*/ |
32
|
4 |
|
public function create(?array $ops = null): Route |
33
|
|
|
{ |
34
|
4 |
|
$component = new Route(); |
35
|
4 |
|
$this->init($component, $ops); |
36
|
4 |
|
$this->validate($component); |
37
|
4 |
|
return $component; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @inheritdoc |
42
|
|
|
*/ |
43
|
5 |
|
protected static function defaultOps(): array |
44
|
|
|
{ |
45
|
|
|
return [ |
46
|
5 |
|
'route' => null, |
47
|
|
|
'content' => null, |
48
|
|
|
'redirect' => null |
49
|
|
|
]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param RouteAwareInterface $entity |
54
|
|
|
* @param int|null $postfix |
55
|
|
|
* @return Route|null |
56
|
|
|
*/ |
57
|
3 |
|
public function createFromRouteAwareEntity(RouteAwareInterface $entity, int $postfix = 0): ?Route |
58
|
|
|
{ |
59
|
3 |
|
$unsanitizedRoute = $entity->getDefaultRoute(); |
60
|
3 |
|
if (false === $unsanitizedRoute) { |
|
|
|
|
61
|
|
|
return null; |
62
|
|
|
} |
63
|
3 |
|
$pageRoute = $this->slugify->slugify($unsanitizedRoute ?: ''); |
64
|
3 |
|
$routePrefix = $this->getRoutePrefix($entity); |
65
|
3 |
|
$fullRoute = $routePrefix . $pageRoute; |
66
|
3 |
|
if ($postfix > 0) { |
67
|
|
|
$fullRoute .= '-' . $postfix; |
68
|
|
|
} |
69
|
3 |
|
$existing = $this->manager->getRepository(Route::class)->find($fullRoute); |
70
|
3 |
|
if ($existing) { |
71
|
|
|
return $this->createFromRouteAwareEntity($entity, $postfix + 1); |
72
|
|
|
} |
73
|
3 |
|
return $this->create( |
74
|
|
|
[ |
75
|
3 |
|
'route' => $fullRoute, |
76
|
3 |
|
'content' => $entity |
77
|
|
|
] |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param RouteAwareInterface $entity |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
3 |
|
private function getRoutePrefix(RouteAwareInterface $entity): string |
86
|
|
|
{ |
87
|
3 |
|
$parent = method_exists($entity, 'getParent') ? $entity->getParent() : null; |
88
|
3 |
|
if ($parent && $parent instanceof RouteAwareInterface) { |
89
|
2 |
|
$parentRoute = $this->getParentRoute($parent); |
90
|
2 |
|
return $parentRoute->getRoute() . '/'; |
91
|
|
|
} |
92
|
3 |
|
return '/'; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param RouteAwareInterface $parent |
97
|
|
|
* @return mixed|Route |
98
|
|
|
*/ |
99
|
2 |
|
private function getParentRoute(RouteAwareInterface $parent) |
100
|
|
|
{ |
101
|
2 |
|
$parentRoute = $parent->getRoutes()->first(); |
102
|
2 |
|
if (!$parentRoute) { |
103
|
1 |
|
$parentRoute = $this->createFromRouteAwareEntity($parent); |
104
|
|
|
} |
105
|
2 |
|
return $parentRoute; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|