Completed
Push — master ( 4da1f4...8b580e )
by Paweł
08:27
created

RouteProvider::getChildrenByStaticPrefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher Content Bundle.
5
 *
6
 * Copyright 2016 Sourcefabric z.ú. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2016 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Bundle\ContentBundle\Provider\ORM;
16
17
use SWP\Bundle\ContentBundle\Model\RouteRepositoryInterface;
18
use SWP\Bundle\ContentBundle\Provider\RouteProviderInterface;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\Routing\Exception\RouteNotFoundException;
21
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm\RouteProvider as BaseRouteProvider;
22
use Doctrine\Common\Persistence\ManagerRegistry;
23
use Symfony\Cmf\Component\Routing\Candidates\CandidatesInterface;
24
use Symfony\Component\Routing\RouteCollection;
25
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm\Route;
26
27
class RouteProvider extends BaseRouteProvider implements RouteProviderInterface
28
{
29
    /**
30
     * @var RouteRepositoryInterface
31
     */
32
    private $routeRepository;
33
34
    /**
35
     * @var array
36
     */
37
    private $internalRoutesCache;
38
39
    /**
40
     * @var CandidatesInterface
41
     */
42 109
    private $candidatesStrategy;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
43
44
    /**
45
     * RouteProvider constructor.
46
     *
47
     * @param RouteRepositoryInterface $routeRepository
48 109
     * @param ManagerRegistry          $managerRegistry
49 109
     * @param CandidatesInterface      $candidatesStrategy
50 109
     * @param string                   $className
51
     */
52 109
    public function __construct(RouteRepositoryInterface $routeRepository, ManagerRegistry $managerRegistry, CandidatesInterface $candidatesStrategy, string $className)
53 109
    {
54
        $this->routeRepository = $routeRepository;
55
        $this->internalRoutesCache = [];
56
        $this->candidatesStrategy = $candidatesStrategy;
57
58
        parent::__construct($managerRegistry, $candidatesStrategy, $className);
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function getRouteCollectionForRequest(Request $request)
65
    {
66
        $collection = new RouteCollection();
67
68
        $candidates = $this->candidatesStrategy->getCandidates($request);
69
        if (0 === count($candidates)) {
70
            return $collection;
71 7
        }
72
        // As we use Gedmo Sortable on position field, we need to reverse sorting to get child routes first
73 7
        $routes = $this->getByStaticPrefix($candidates, ['level' => 'DESC', 'position' => 'DESC']);
74
75
        /** @var $route Route */
76
        foreach ($routes as $route) {
77
            $collection->add($route->getName(), $route);
78
        }
79 2
80
        return $collection;
81 2
    }
82
83
    /**
84
     * @return RouteRepositoryInterface
85
     */
86
    public function getRepository(): RouteRepositoryInterface
87 11
    {
88
        return $this->routeRepository;
89 11
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function getBaseRoute()
95 94
    {
96
        throw new \Exception('Not implemented in ORM');
97 94
    }
98 56
99
    /**
100
     * {@inheritdoc}
101 94
     */
102
    public function getOneById($id)
103
    {
104
        return $this->routeRepository->findOneBy(['id' => $id]);
105 94
    }
106 94
107 94
    /**
108 78
     * {@inheritdoc}
109
     */
110
    public function getOneByStaticPrefix($staticPrefix)
111 23
    {
112
        return $this->routeRepository->findOneBy(['staticPrefix' => $staticPrefix]);
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function getByStaticPrefix(array $candidates, array $orderBy = []): array
119
    {
120
        return $this->getRouteRepository()->findBy(['staticPrefix' => $candidates], $orderBy);
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function getChildrenByStaticPrefix(array $candidates, array $orderBy = []): array
127
    {
128
        return $this->getRepository()->getChildrenByStaticPrefix($candidates, $orderBy)->getQuery()->getResult();
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134
    public function getWithChildrenByStaticPrefix(array $candidates): ?array
135
    {
136
        $routes = null;
0 ignored issues
show
Unused Code introduced by
$routes is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
137
        $routesForChilldrensLoading = [];
138
        foreach ($candidates as $key => $providedRoute) {
139
            if (false !== strpos($providedRoute, '/*')) {
140
                $cleanRouteName = str_replace('/*', '', $providedRoute);
141
                $routesForChilldrensLoading[$cleanRouteName] = null;
142
                $candidates[$key] = $cleanRouteName;
143
            }
144
        }
145
146
        $routesArray = $this->getByStaticPrefix($candidates);
147
        if (count($routesArray) <= 0) {
148
            return null;
149
        }
150
151
        $routes = $this->getArrayOfIdsFromRoutesArray($routesArray);
152
153
        if (count($routesForChilldrensLoading) > 0) {
154
            foreach ($routesArray as $key => $element) {
155
                if (array_key_exists($element->getStaticPrefix(), $routesForChilldrensLoading)) {
156
                    $routesForChilldrensLoading[$element->getStaticPrefix()] = $element->getId();
157
                }
158
            }
159
160
            $routesForChilldrensLoading = array_filter(array_values($routesForChilldrensLoading));
161
            $childrenRoutesArray = $this->getChildrenByStaticPrefix($routesForChilldrensLoading);
162
            if (count($childrenRoutesArray) > 0) {
163
                $routes = array_merge($routes, $this->getArrayOfIdsFromRoutesArray($childrenRoutesArray));
164
            }
165
        }
166
167
        return $routes;
168
    }
169
170
    /**
171
     * {@inheritdoc}
172
     */
173
    public function getRouteByName($name)
174
    {
175
        if (array_key_exists($name, $this->internalRoutesCache)) {
176
            return $this->internalRoutesCache[$name];
177
        }
178
179
        if (!$this->candidatesStrategy->isCandidate($name)) {
180
            throw new RouteNotFoundException(sprintf('Route "%s" is not handled by this route provider', $name));
181
        }
182
183
        $route = $this->getRouteRepository()->findOneBy(array('name' => $name));
184
        $this->internalRoutesCache[$name] = $route;
185
        if (!$route) {
186
            throw new RouteNotFoundException("No route found for name '$name'");
187
        }
188
189
        return $route;
190
    }
191
192
    /**
193
     * @param array $routes
194
     *
195
     * @return array
196
     */
197
    private function getArrayOfIdsFromRoutesArray(array $routes): array
198
    {
199
        return array_map(function ($route) {
200
            return $route->getId();
201
        }, $routes);
202
    }
203
}
204