ChainUrlGenerator::setContext()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Skalpa\Silex\Symfony\Routing;
4
5
use Symfony\Component\Routing\Exception\RouteNotFoundException;
6
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
7
use Symfony\Component\Routing\RequestContext;
8
9
10
/**
11
 * Generates URLs using other generators.
12
 */
13
class ChainUrlGenerator implements UrlGeneratorInterface
14
{
15
    private $context;
16
    private $generators;
17
18
    /**
19
     * @param UrlGeneratorInterface[] $generators
20
     * @param RequestContext          $context
21
     */
22 9
    public function __construct(array $generators, RequestContext $context)
23
    {
24 9
        foreach ($generators as $generator) {
25 9 View Code Duplication
            if (!$generator instanceof UrlGeneratorInterface) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
26 1
                throw new \InvalidArgumentException(sprintf('Invalid URL generator. Expected an instance of %s, got %s.', UrlGeneratorInterface::class, is_object($generator) ? get_class($generator) : gettype($generator)));
27
            }
28 8
            $generator->setContext($context);
29 8
        }
30 8
        $this->generators = $generators;
31 8
        $this->context = $context;
32 8
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 1
    public function setContext(RequestContext $context)
38
    {
39 1
        foreach ($this->generators as $generator) {
40 1
            $generator->setContext($context);
41 1
        }
42 1
        $this->context = $context;
43 1
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 2
    public function getContext()
49
    {
50 2
        return $this->context;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 6
    public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
57
    {
58
        /** @var RouteNotFoundException $notFound */
59 6
        $notFound = null;
60
61 6
        foreach ($this->generators as $generator) {
62
            try {
63 6
                return $generator->generate($name, $parameters, $referenceType);
64 3
            } catch (RouteNotFoundException $e) {
65 3
                $notFound = $e;
66
            }
67 3
        }
68 1
        throw $notFound;
69
    }
70
}
71