Passed
Push — develop ( d3ab5a...c8bc8a )
by Daniel
07:00
created

RouteFactory::createFromRouteAwareEntity()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4.0466

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 4
nop 2
dl 0
loc 20
ccs 12
cts 14
cp 0.8571
crap 4.0466
rs 9.2
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\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
56
     */
57 3
    public function createFromRouteAwareEntity(RouteAwareInterface $entity, int $postfix = 0): Route
58
    {
59 3
        $pageRoute = $this->slugify->slugify($entity->getDefaultRoute() ?: '');
60 3
        $routePrefix = $this->getRoutePrefix($entity);
61 3
        $fullRoute = $routePrefix . $pageRoute;
62 3
        if ($postfix > 0) {
63
            $fullRoute .= '-' . $postfix;
64
        }
65 3
        $existing = $this->manager->getRepository(Route::class)->find($fullRoute);
66 3
        if ($existing) {
67
            return $this->createFromRouteAwareEntity($entity, $postfix + 1);
68
        }
69 3
        $route = $this->create(
70
            [
71 3
                'route' => $fullRoute,
72 3
                'content' => $entity
73
            ]
74
        );
75 3
        $entity->addRoute($route);
76 3
        return $route;
77
    }
78
79
    /**
80
     * @param RouteAwareInterface $entity
81
     * @return string
82
     */
83 3
    private function getRoutePrefix(RouteAwareInterface $entity): string
84
    {
85 3
        $parent = method_exists($entity, 'getParent') ? $entity->getParent() : null;
86 3
        if ($parent && $parent instanceof RouteAwareInterface) {
87 2
            $parentRoute = $this->getParentRoute($parent);
88 2
            return $parentRoute->getRoute() . '/';
89
        }
90 3
        return '/';
91
    }
92
93
    /**
94
     * @param RouteAwareInterface $parent
95
     * @return mixed|Route
96
     */
97 2
    private function getParentRoute(RouteAwareInterface $parent)
98
    {
99 2
        $parentRoute = $parent->getRoutes()->first();
100 2
        if (!$parentRoute) {
101 1
            $parentRoute = $this->createFromRouteAwareEntity($parent);
102
        }
103 2
        return $parentRoute;
104
    }
105
}
106