Passed
Push — master ( d73b0c...81a6dc )
by Daniel
06:35
created

RouteGenerator::resolveConflicts()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 9
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 14
ccs 0
cts 10
cp 0
crap 6
rs 9.9666
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentsBundle\Helper\Route;
15
16
use Cocur\Slugify\SlugifyInterface;
17
use Doctrine\Persistence\ManagerRegistry;
18
use Silverback\ApiComponentsBundle\Entity\Core\RoutableInterface;
19
use Silverback\ApiComponentsBundle\Entity\Core\Route;
20
use Silverback\ApiComponentsBundle\Exception\InvalidArgumentException;
21
use Silverback\ApiComponentsBundle\Helper\Timestamped\TimestampedDataPersister;
22
use Silverback\ApiComponentsBundle\Repository\Core\RouteRepository;
23
24
/**
25
 * @author Daniel West <[email protected]>
26
 */
27
class RouteGenerator implements RouteGeneratorInterface
28
{
29
    private SlugifyInterface $slugify;
30
    private ManagerRegistry $registry;
31
    private TimestampedDataPersister $timestampedDataPersister;
32
    private RouteRepository $routeRepository;
33
34
    public function __construct(
35
        SlugifyInterface $slugify,
36
        ManagerRegistry $registry,
37
        TimestampedDataPersister $timestampedDataPersister,
38
        RouteRepository $routeRepository
39
    ) {
40
        $this->slugify = $slugify;
41
        $this->registry = $registry;
42
        $this->timestampedDataPersister = $timestampedDataPersister;
43
        $this->routeRepository = $routeRepository;
44
    }
45
46
    public function createRedirect(string $fromPath, Route $targetRoute): Route
47
    {
48
        [$name] = $this->resolveConflicts($fromPath, $fromPath);
49
50
        $newRedirect = new Route();
51
        $newRedirect
52
            ->setName($name)
53
            ->setPath($fromPath)
54
            ->setRedirect($targetRoute);
55
        $this->timestampedDataPersister->persistTimestampedFields($newRedirect, true);
56
57
        return $newRedirect;
58
    }
59
60
    public function create(RoutableInterface $object, ?Route $route = null): Route
61
    {
62
        $entityManager = $this->registry->getManagerForClass($className = \get_class($object));
63
        if (!$entityManager) {
64
            throw new InvalidArgumentException(sprintf('Could not find entity manager for %s', $className));
65
        }
66
        $uow = $entityManager->getUnitOfWork();
0 ignored issues
show
Bug introduced by
The method getUnitOfWork() does not exist on Doctrine\Persistence\ObjectManager. It seems like you code against a sub-type of said class. However, the method does not exist in Doctrine\Persistence\ObjectManagerDecorator. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
        /** @scrutinizer ignore-call */ 
67
        $uow = $entityManager->getUnitOfWork();
Loading history...
67
        /** @var RoutableInterface $originalPage */
68
        $originalPage = $uow->getOriginalEntityData($object);
69
        $existingRoute = $originalPage['route'] ?? null;
70
71
        $isNew = !((bool) $route);
72
        $route = $route ?? new Route();
73
74
        $this->timestampedDataPersister->persistTimestampedFields($route, $isNew);
75
        $titleSlug = $this->slugify->slugify($object->getTitle());
76
        $name = $titleSlug;
77
78
        $path = '/' . ltrim($titleSlug, '/');
79
80
        if ($parentRoute = $object->getParentRoute()) {
81
            $path = '/' . ltrim($parentRoute->getPath(), '/') . $path;
82
        }
83
84
        [$name, $path] = $this->resolveConflicts($name, $path);
85
86
        $route
87
            ->setName($name)
88
            ->setPath($path);
89
        $object->setRoute($route);
90
91
        if ($existingRoute) {
92
            $existingRoute->setRedirect($route);
93
            // When we enabled patch endpoint for route, this was required.
94
            // The existing route is found in uow, perhaps this is why..
95
            // Future investigation would be nice to know reasoning for this breaking tests and pageData becoming null
96
            // on the $route and staying on the existingRoute only when patch enabled.
97
            $route->setPage($existingRoute->getPage());
98
            $route->setpageData($existingRoute->getPageData());
99
        }
100
101
        return $route;
102
    }
103
104
    private function resolveConflicts(string $name, string $path): array
105
    {
106
        $conflicts = $this->routeRepository->findConflicts($name, $path);
107
108
        $baseName = $name;
109
        $basePath = $path;
110
        $conflictCounter = 0;
111
        while ($this->conflictExists($name, $path, $conflicts)) {
112
            ++$conflictCounter;
113
            $name = sprintf('%s-%d', $baseName, $conflictCounter);
114
            $path = sprintf('%s-%d', $basePath, $conflictCounter);
115
        }
116
117
        return [$name, $path];
118
    }
119
120
    /**
121
     * @param Route[] $conflicts
122
     */
123
    private function conflictExists(string $name, string $path, array $conflicts): bool
124
    {
125
        foreach ($conflicts as $conflict) {
126
            if ($conflict->getPath() === $path) {
127
                return true;
128
            }
129
            if ($conflict->getName() === $name) {
130
                return true;
131
            }
132
        }
133
134
        return false;
135
    }
136
}
137