Completed
Push — master ( a70ca2...b8c2a5 )
by Daniel
14:22
created

MethodMatcher   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 13
c 2
b 1
f 1
lcom 1
cbo 3
dl 0
loc 74
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A match() 0 13 3
A matchCollection() 0 13 3
A getAttributes() 0 6 1
A setContext() 0 4 1
A getContext() 0 4 1
A mergeDefaults() 0 10 3
1
<?php
2
3
namespace Cmobi\RabbitmqBundle\Routing\Matcher;
4
5
use Cmobi\RabbitmqBundle\Routing\Method;
6
use Cmobi\RabbitmqBundle\Routing\MethodCollection;
7
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
8
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
9
use Symfony\Component\Routing\RequestContext;
10
11
class MethodMatcher implements MethodMatcherInterface
12
{
13
    protected $context;
14
    protected $methods;
15
    protected $allow = [];
16
17
    public function __construct(MethodCollection $methods, Method $context)
18
    {
19
        $this->methods = $methods;
20
        $this->context = $context;
21
    }
22
23
    public function match($path)
24
    {
25
        $this->allow = [];
26
27
        if ($ret = $this->matchCollection($path, $this->methods)) {
28
            return $ret;
29
        }
30
31
        if (0 < count($this->allow)) {
32
            throw new MethodNotAllowedException(array_unique($this->allow));
33
        }
34
        throw new ResourceNotFoundException(sprintf('No routes found for "%s".', $path));
35
    }
36
37
    public function matchCollection($path, MethodCollection $methods)
38
    {
39
        foreach ($methods as $name => $method) {
40
41
            if ($method->getName() !== $path) {
42
                continue;
43
            }
44
45
            return $this->getAttributes($method, $name, $method->getOptions());
46
        }
47
48
        return [];
49
    }
50
51
    protected function getAttributes(Method $method, $name, array $attributes)
52
    {
53
        $attributes['_method'] = $name;
54
55
        return $this->mergeDefaults($attributes, $method->getDefaults());
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function setContext(RequestContext $context)
62
    {
63
        $this->context = $context;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function getContext()
70
    {
71
        return $this->context;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->context; of type Cmobi\RabbitmqBundle\Rou...\Routing\RequestContext adds the type Cmobi\RabbitmqBundle\Routing\Method to the return on line 71 which is incompatible with the return type declared by the interface Symfony\Component\Routin...reInterface::getContext of type Symfony\Component\Routing\RequestContext.
Loading history...
72
    }
73
74
    protected function mergeDefaults($params, $defaults)
75
    {
76
        foreach ($params as $key => $value) {
77
            if (!is_int($key)) {
78
                $defaults[$key] = $value;
79
            }
80
        }
81
82
        return $defaults;
83
    }
84
}
85