Completed
Push — master ( a682ba...4851b3 )
by Paweł
40:50
created

RouteProvider   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 80.77%

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 6
dl 0
loc 89
ccs 21
cts 26
cp 0.8077
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A getRepository() 0 4 1
A getBaseRoute() 0 4 1
A getOneById() 0 4 1
A getOneByStaticPrefix() 0 4 1
A getRouteForArticle() 0 4 1
A getRouteByName() 0 18 4
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\ArticleInterface;
18
use SWP\Bundle\ContentBundle\Model\RouteRepositoryInterface;
19
use SWP\Bundle\ContentBundle\Provider\RouteProviderInterface;
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
25
class RouteProvider extends BaseRouteProvider implements RouteProviderInterface
26
{
27
    /**
28
     * @var RouteRepositoryInterface
29
     */
30
    private $routeRepository;
31
32
    /**
33
     * @var array
34
     */
35
    private $internalRoutesCache;
36
37
    /**
38
     * @var CandidatesInterface
39
     */
40
    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...
41
42 105
    public function __construct(
43
        RouteRepositoryInterface $routeRepository,
44
        ManagerRegistry $managerRegistry,
45
        CandidatesInterface $candidatesStrategy,
46
        $className
47
    ) {
48 105
        $this->routeRepository = $routeRepository;
49 105
        $this->internalRoutesCache = [];
50 105
        $this->candidatesStrategy = $candidatesStrategy;
51
52 105
        parent::__construct($managerRegistry, $candidatesStrategy, $className);
53 105
    }
54
55
    public function getRepository(): RouteRepositoryInterface
56
    {
57
        return $this->routeRepository;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function getBaseRoute()
64
    {
65
        throw new \Exception('Not implemented in ORM');
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 14
    public function getOneById($id)
72
    {
73 14
        return $this->routeRepository->findOneBy(['id' => $id]);
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 6
    public function getOneByStaticPrefix($staticPrefix)
80
    {
81 6
        return $this->routeRepository->findOneBy(['staticPrefix' => $staticPrefix]);
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 16
    public function getRouteForArticle(ArticleInterface $article)
88
    {
89 16
        return $article->getRoute();
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 88
    public function getRouteByName($name)
96
    {
97 88
        if (array_key_exists($name, $this->internalRoutesCache)) {
98 63
            return $this->internalRoutesCache[$name];
99
        }
100
101 88
        if (!$this->candidatesStrategy->isCandidate($name)) {
102
            throw new RouteNotFoundException(sprintf('Route "%s" is not handled by this route provider', $name));
103
        }
104
105 88
        $route = $this->getRouteRepository()->findOneBy(array('name' => $name));
106 88
        $this->internalRoutesCache[$name] = $route;
107 88
        if (!$route) {
108 69
            throw new RouteNotFoundException("No route found for name '$name'");
109
        }
110
111 32
        return $route;
112
    }
113
}
114