Completed
Push — master ( 7a069b...4da1f4 )
by Paweł
11s
created

RouteProvider::getRouteForArticle()   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
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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 111
    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 111
     * @param ManagerRegistry          $managerRegistry
49 111
     * @param CandidatesInterface      $candidatesStrategy
50 111
     * @param string                   $className
51
     */
52 111
    public function __construct(RouteRepositoryInterface $routeRepository, ManagerRegistry $managerRegistry, CandidatesInterface $candidatesStrategy, string $className)
53 111
    {
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 8
        }
72
        // As we use Gedmo Sortable on position field, we need to reverse sorting to get child routes first
73 8
        $routes = $this->getRouteRepository()->findByStaticPrefix($candidates, ['level' => 'DESC', 'position' => 'DESC']);
0 ignored issues
show
Bug introduced by
The method findByStaticPrefix() does not exist on Doctrine\Common\Persistence\ObjectRepository. Did you maybe mean findBy()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
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 12
    {
88
        return $this->routeRepository;
89 12
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function getBaseRoute()
95 96
    {
96
        throw new \Exception('Not implemented in ORM');
97 96
    }
98 58
99
    /**
100
     * {@inheritdoc}
101 96
     */
102
    public function getOneById($id)
103
    {
104
        return $this->routeRepository->findOneBy(['id' => $id]);
105 96
    }
106 96
107 96
    /**
108 80
     * {@inheritdoc}
109
     */
110
    public function getOneByStaticPrefix($staticPrefix)
111 24
    {
112
        return $this->routeRepository->findOneBy(['staticPrefix' => $staticPrefix]);
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function getRouteByName($name)
119
    {
120
        if (array_key_exists($name, $this->internalRoutesCache)) {
121
            return $this->internalRoutesCache[$name];
122
        }
123
124
        if (!$this->candidatesStrategy->isCandidate($name)) {
125
            throw new RouteNotFoundException(sprintf('Route "%s" is not handled by this route provider', $name));
126
        }
127
128
        $route = $this->getRouteRepository()->findOneBy(array('name' => $name));
129
        $this->internalRoutesCache[$name] = $route;
130
        if (!$route) {
131
            throw new RouteNotFoundException("No route found for name '$name'");
132
        }
133
134
        return $route;
135
    }
136
}
137