Passed
Push — master ( 958c9b...b15d31 )
by Daniel
06:43
created

RouteGenerator::create()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 3
nop 2
dl 0
loc 25
ccs 0
cts 15
cp 0
crap 12
rs 9.7666
c 0
b 0
f 0
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\AbstractPage;
19
use Silverback\ApiComponentsBundle\Entity\Core\RoutableInterface;
20
use Silverback\ApiComponentsBundle\Entity\Core\Route;
21
use Silverback\ApiComponentsBundle\Exception\InvalidArgumentException;
22
23
/**
24
 * @author Daniel West <[email protected]>
25
 */
26
class RouteGenerator implements RouteGeneratorInterface
27
{
28
    private SlugifyInterface $slugify;
29
    private ManagerRegistry $registry;
30
31
    public function __construct(SlugifyInterface $slugify, ManagerRegistry $registry)
32
    {
33
        $this->slugify = $slugify;
34
        $this->registry = $registry;
35
    }
36
37
    public function create(RoutableInterface $object, ?Route $route = null): Route
38
    {
39
        $entityManager = $this->registry->getManagerForClass($className = \get_class($object));
40
        if (!$entityManager) {
41
            throw new InvalidArgumentException(sprintf('Could not find entity manager for %s', $className));
42
        }
43
        $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 Doctrine\Persistence\ObjectManager such as Doctrine\ORM\EntityManagerInterface or Doctrine\ORM\Decorator\EntityManagerDecorator. ( Ignorable by Annotation )

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

43
        /** @scrutinizer ignore-call */ 
44
        $uow = $entityManager->getUnitOfWork();
Loading history...
44
        /** @var AbstractPage $originalPage */
45
        $originalPage = $uow->getOriginalEntityData($object);
46
        $existingRoute = $originalPage['route'];
47
48
        $route = $route ?? new Route();
49
50
        $path = $this->slugify->slugify($object->getTitle());
51
52
        $route
53
            ->setName(sprintf('generated-%s', $path))
54
            ->setPath('/' . ltrim($path, '/'));
55
        $object->setRoute($route);
56
57
        if ($existingRoute) {
58
            $existingRoute->setRedirect($route);
59
        }
60
61
        return $route;
62
    }
63
}
64