Test Failed
Push — master ( 9ceea8...5b7b5b )
by Daniel
09:29
created

RouteRepository   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 35.29%

Importance

Changes 0
Metric Value
eloc 30
c 0
b 0
f 0
dl 0
loc 60
rs 10
ccs 12
cts 34
cp 0.3529
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A findConflicts() 0 13 1
A findOneByIdOrPath() 0 19 3
A findByPageData() 0 14 1
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\Repository\Core;
15
16
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
17
use Doctrine\ORM\Query\Expr\Join;
18
use Doctrine\Persistence\ManagerRegistry;
19
use Ramsey\Uuid\Exception\InvalidUuidStringException;
20
use Ramsey\Uuid\Uuid;
21
use Silverback\ApiComponentsBundle\Entity\Core\AbstractPageData;
22
use Silverback\ApiComponentsBundle\Entity\Core\Route;
23
24
/**
25
 * @author Daniel West <[email protected]>
26
 *
27
 * @method Route|null find($id, $lockMode = null, $lockVersion = null)
28
 * @method Route|null findOneBy(array $criteria, array $orderBy = null)
29
 * @method Route[]    findAll()
30
 * @method Route[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
31
 */
32
class RouteRepository extends ServiceEntityRepository
33
{
34
    public function __construct(ManagerRegistry $registry)
35 1
    {
36
        parent::__construct($registry, Route::class);
37 1
    }
38 1
39
    public function findOneByIdOrPath(string $idOrRoute): ?Route
40 1
    {
41
        $route = $this->findOneBy(
42 1
            [
43
                'path' => $idOrRoute,
44 1
            ]
45
        );
46
        if ($route) {
47 1
            return $route;
48 1
        }
49
50
        try {
51
            $uuid = Uuid::fromString($idOrRoute);
52 1
53
            return $this->find($uuid);
54 1
        } catch (InvalidUuidStringException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
55 1
        }
56
57
        return null;
58 1
    }
59
60
    /**
61
     * @return Route[]
62
     */
63
    public function findByPageData(AbstractPageData $pageData): array
64
    {
65
        $queryBuilder = $this->createQueryBuilder('route');
66
        $queryBuilder
67
            ->leftJoin(
68
                'route.pageData',
69
                'pageData',
70
                Join::WITH,
71
                $queryBuilder->expr()->eq('route', 'pageData.route')
72
            )
73
            ->andWhere($queryBuilder->expr()->eq('pageData', ':page_data'))
74
            ->setParameter('page_data', $pageData);
75
76
        return $queryBuilder->getQuery()->getResult();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $queryBuilder->getQuery()->getResult() could return the type integer which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
77
    }
78
79
    public function findConflicts(string $name, string $path): array
80
    {
81
        $queryBuilder = $this->createQueryBuilder('route');
82
        $expr = $queryBuilder->expr();
83
        $queryBuilder
84
            ->andWhere($expr->orX(
85
                $expr->like('route.path', ':path'),
86
                $expr->like('route.name', ':name'),
87
            ))
88
            ->setParameter('path', $path . '%')
89
            ->setParameter('name', $name . '%');
90
91
        return $queryBuilder->getQuery()->getResult();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $queryBuilder->getQuery()->getResult() could return the type integer which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
92
    }
93
}
94