Passed
Push — master ( 3cc058...1db5d3 )
by Daniel
06:26
created

RouteRepository::findConflicts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 9
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 13
ccs 0
cts 9
cp 0
crap 2
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\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\AbstractComponent;
22
use Silverback\ApiComponentsBundle\Entity\Core\AbstractPageData;
23
use Silverback\ApiComponentsBundle\Entity\Core\Route;
24
25
/**
26
 * @author Daniel West <[email protected]>
27
 *
28
 * @method Route|null find($id, $lockMode = null, $lockVersion = null)
29
 * @method Route|null findOneBy(array $criteria, array $orderBy = null)
30
 * @method Route[]    findAll()
31
 * @method Route[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
32
 */
33
class RouteRepository extends ServiceEntityRepository
34
{
35 8
    public function __construct(ManagerRegistry $registry)
36
    {
37 8
        parent::__construct($registry, Route::class);
38 8
    }
39
40 1
    public function findOneByIdOrPath(string $idOrRoute)
41
    {
42 1
        $route = $this->findOneBy(
43
            [
44 1
                'path' => $idOrRoute,
45
            ]
46
        );
47 1
        if ($route) {
48 1
            return $route;
49
        }
50
51
        try {
52 1
            $uuid = Uuid::fromString($idOrRoute);
53 1
        } catch (InvalidUuidStringException $e) {
54 1
            return null;
55
        }
56
57 1
        return $this->find($uuid);
58
    }
59
60
    /**
61
     * @return Route[]
62
     */
63
    public function findByComponent(AbstractComponent $component): 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
74
            ->leftJoin(
75
                'pageData.page',
76
                'pageData_page',
77
                Join::WITH,
78
                $queryBuilder->expr()->eq('pageData_page', 'pageData.page')
79
            )
80
            ->leftJoin(
81
                'pageData_page.componentCollections',
82
                'page_data_cc'
83
            )
84
            ->leftJoin(
85
                'page_data_cc.componentPositions',
86
                'page_data_pos'
87
            )
88
            ->leftJoin(
89
                'page_data_pos.component',
90
                'page_data_component',
91
                Join::WITH,
92
                $queryBuilder->expr()->eq('page_data_pos.component', 'page_data_component')
93
            )
94
95
            ->leftJoin(
96
                'route.page',
97
                'page',
98
                Join::WITH,
99
                $queryBuilder->expr()->eq('route', 'page.route')
100
            )
101
            ->leftJoin(
102
                'page.componentCollections',
103
                'page_cc'
104
            )
105
            ->leftJoin(
106
                'page_cc.componentPositions',
107
                'page_pos'
108
            )
109
            ->leftJoin(
110
                'page_pos.component',
111
                'page_component',
112
                Join::WITH,
113
                $queryBuilder->expr()->eq('page_pos.component', 'page_component')
114
            )
115
116
            ->andWhere(
117
                $queryBuilder->expr()->orX(
118
                    $queryBuilder->expr()->eq('page_component', ':component'),
119
                    $queryBuilder->expr()->eq('page_data_component', ':component')
120
                )
121
            )
122
            ->setParameter('component', $component);
123
124
        return $queryBuilder->getQuery()->getResult();
125
    }
126
127
    /**
128
     * @return Route[]
129
     */
130
    public function findByPageData(AbstractPageData $pageData): array
131
    {
132
        $queryBuilder = $this->createQueryBuilder('route');
133
        $queryBuilder
134
            ->leftJoin(
135
                'route.pageData',
136
                'pageData',
137
                Join::WITH,
138
                $queryBuilder->expr()->eq('route', 'pageData.route')
139
            )
140
            ->andWhere($queryBuilder->expr()->eq('pageData', ':page_data'))
141
            ->setParameter('page_data', $pageData);
142
143
        return $queryBuilder->getQuery()->getResult();
144
    }
145
146
    public function findConflicts(string $name, string $path): array
147
    {
148
        $queryBuilder = $this->createQueryBuilder('route');
149
        $expr = $queryBuilder->expr();
150
        $queryBuilder
151
            ->andWhere($expr->orX(
152
                $expr->like('route.path', ':path'),
153
                $expr->like('route.name', ':name'),
154
            ))
155
            ->setParameter('path', $path . '%')
156
            ->setParameter('name', $name . '%');
157
158
        return $queryBuilder->getQuery()->getResult();
159
    }
160
}
161