Completed
Push — develop ( e6c657...69ffd6 )
by Daniel
07:10
created

RouteFactory::getParentRoute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
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\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
11
use Symfony\Component\Validator\Validator\ValidatorInterface;
12
13
class RouteFactory extends AbstractFactory
14
{
15
    /**
16
     * @var SlugifyInterface
17
     */
18
    private $slugify;
19
20 7
    public function __construct(
21
        ObjectManager $manager,
22
        ValidatorInterface $validator,
23
        SlugifyInterface $slugify
24
    )
25
    {
26 7
        $this->slugify = $slugify;
27 7
        parent::__construct($manager, $validator);
28 7
    }
29
30
    /**
31
     * @inheritdoc
32
     */
33 4
    public function create(?array $ops = null): Route
34
    {
35 4
        $component = new Route();
36 4
        $this->init($component, $ops);
37 4
        $this->validate($component);
38 4
        return $component;
39
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44 5
    protected static function defaultOps(): array
45
    {
46
        return [
47 5
            'name' => null,
48
            'route' => null,
49
            'content' => null,
50
            'redirect' => null
51
        ];
52
    }
53
54
    /**
55
     * @param RouteAwareInterface $entity
56
     * @param int|null $postfix
57
     * @return Route
58
     */
59 3
    public function createFromRouteAwareEntity(RouteAwareInterface $entity, int $postfix = 0): Route
60
    {
61 3
        $pageRoute = $this->slugify->slugify($entity->getDefaultRoute() ?: '');
62 3
        $routePrefix = $this->getRoutePrefix($entity);
63 3
        $fullRoute = $routePrefix . $pageRoute;
64 3
        if ($postfix > 0) {
65
            $fullRoute .= '-' . $postfix;
66
        }
67 3
        $existing = $this->manager->getRepository(Route::class)->find($fullRoute);
68 3
        if ($existing) {
69
            return $this->createFromRouteAwareEntity($entity, $postfix + 1);
70
        }
71 3
        $converter = new CamelCaseToSnakeCaseNameConverter();
72 3
        $route = $this->create(
73
            [
74 3
                'name' => $converter->normalize(str_replace(' ', '', $entity->getDefaultRouteName())),
75 3
                'route' => $fullRoute,
76 3
                'content' => $entity
77
            ]
78
        );
79 3
        $entity->addRoute($route);
80 3
        return $route;
81
    }
82
83
    /**
84
     * @param RouteAwareInterface $entity
85
     * @return string
86
     */
87 3
    private function getRoutePrefix(RouteAwareInterface $entity): string
88
    {
89 3
        $parent = $entity->getParentRoute();
90 3
        if ($parent) {
91 2
            return $parent->getRoute() . '/';
92
        }
93 2
        return '/';
94
    }
95
}
96