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

PhpMatcherDumper::dump()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 38
rs 8.8571
cc 1
eloc 11
nc 1
nop 1
1
<?php
2
3
namespace Cmobi\RabbitmqBundle\Routing\Matcher\Dumper;
4
5
use Cmobi\RabbitmqBundle\Routing\MethodCollection;
6
7
class PhpMatcherDumper extends MatcherDumper
8
{
9
10
    public function dump(array $options = [])
11
    {
12
        $options = array_replace(
13
            [
14
                'class' => 'ProjectMethodMatcher',
15
                'base_class' => 'Cmobi\\RabbitmqBundle\\Routing\\Matcher\\MethodMatcher',
16
            ],
17
            $options
18
        );
19
20
        return <<<EOF
21
<?php
22
23
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
24
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
25
use Cmobi\RabbitmqBundle\Routing\Method;
26
27
/**
28
 * {$options['class']}.
29
 *
30
 * This class has been auto-generated
31
 * by the Symfony Routing Component.
32
 */
33
class {$options['class']} extends {$options['base_class']}
34
{
35
    /**
36
     * Constructor.
37
     */
38
    public function __construct(Method \$context)
39
    {
40
        \$this->context = \$context;
41
    }
42
43
{$this->generateMatchMethod()}
44
}
45
46
EOF;
47
    }
48
49
    private function generateMatchMethod()
50
    {
51
        $code = rtrim($this->compileRoutes($this->getMethods()), "\n");
52
53
        return <<<EOF
54
    public function match(\$name)
55
    {
56
        \$allow = [];
57
        \$context = \$this->context;
58
59
$code
60
        throw 0 < count(\$allow) ? new MethodNotAllowedException(array_unique(\$allow)) : new ResourceNotFoundException();
61
    }
62
EOF;
63
    }
64
65
    private function compileRoutes(MethodCollection $methods)
66
    {
67
        $code = '';
68
69
        foreach ($methods as $collection) {
70
            if (null !== $name = $collection->getAttribute('name')) {
71
                $code .= sprintf("        if (\$method === \$name)) {\n", var_export($name, true));
72
            }
73
        }
74
75
        return $code;
76
    }
77
78
}
79