Completed
Pull Request — master (#3)
by Mariano
09:11
created

AnnotationParserFactory::getAnnotationParserFor()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
cc 3
eloc 5
nc 3
nop 1
crap 3
1
<?php
2
namespace Mcustiel\SimpleRequest\Strategies;
3
4
use Mcustiel\SimpleRequest\Annotation\RequestAnnotation;
5
use Mcustiel\SimpleRequest\Annotation\ValidatorAnnotation;
6
use Mcustiel\SimpleRequest\Annotation\FilterAnnotation;
7
use Mcustiel\SimpleRequest\Annotation\ParseAs;
8
use Mcustiel\SimpleRequest\Strategies\Annotations\ValidatorAnnotationParser;
9
use Mcustiel\SimpleRequest\Strategies\Annotations\FilterAnnotationParser;
10
use Mcustiel\SimpleRequest\Strategies\Annotations\ParseAsAnnotationParser;
11
12
class AnnotationParserFactory
13
{
14
    private $map = [
15
        ValidatorAnnotation::class => ValidatorAnnotationParser::class,
16
        FilterAnnotation::class    => FilterAnnotationParser::class,
17
        ParseAs::class             => ParseAsAnnotationParser::class,
18
    ];
19
20
    /**
21
     * @param RequestAnnotation $annotation
22
     *
23
     * @throws \Exception
24
     *
25
     * @return \Mcustiel\SimpleRequest\Strategies\Annotations\AnnotationParser
26
     */
27 95
    public function getAnnotationParserFor(RequestAnnotation $annotation)
28
    {
29 95
        foreach ($this->map as $key => $val) {
30 95
            if ($annotation instanceof  $key) {
31 94
                return new $val;
32
            }
33 16
        }
34 1
        throw new \Exception('Unsupported annotation: ' . get_class($annotation));
35
    }
36
}
37