Passed
Push — develop ( 4895f7...2b6cea )
by Daniel
08:09
created

RouteFactory::createFromRouteAwareEntity()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5.2

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 5
nop 2
dl 0
loc 20
ccs 12
cts 15
cp 0.8
crap 5.2
rs 8.8571
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|null
56
     */
57 3
    public function createFromRouteAwareEntity(RouteAwareInterface $entity, int $postfix = 0): ?Route
58
    {
59 3
        $unsanitizedRoute = $entity->getDefaultRoute();
60 3
        if (false === $unsanitizedRoute) {
0 ignored issues
show
introduced by
The condition false === $unsanitizedRoute is always false.
Loading history...
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