Passed
Push — develop ( ebc66f...f9a13b )
by Daniel
05:58
created

RouteFactory::createFromRouteAwareEntity()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.074

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 4
nop 2
dl 0
loc 16
ccs 10
cts 12
cp 0.8333
crap 4.074
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
        return $this->create(
70
            [
71 3
                'route' => $fullRoute,
72 3
                'content' => $entity
73
            ]
74
        );
75
    }
76
77
    /**
78
     * @param RouteAwareInterface $entity
79
     * @return string
80
     */
81 3
    private function getRoutePrefix(RouteAwareInterface $entity): string
82
    {
83 3
        $parent = method_exists($entity, 'getParent') ? $entity->getParent() : null;
84 3
        if ($parent && $parent instanceof RouteAwareInterface) {
85 2
            $parentRoute = $this->getParentRoute($parent);
86 2
            return $parentRoute->getRoute() . '/';
87
        }
88 3
        return '/';
89
    }
90
91
    /**
92
     * @param RouteAwareInterface $parent
93
     * @return mixed|Route
94
     */
95 2
    private function getParentRoute(RouteAwareInterface $parent)
96
    {
97 2
        $parentRoute = $parent->getRoutes()->first();
98 2
        if (!$parentRoute) {
99 1
            $parentRoute = $this->createFromRouteAwareEntity($parent);
100
        }
101 2
        return $parentRoute;
102
    }
103
}
104