Completed
Branch develop (22789e)
by Mariano
08:33
created

ParserGenerator::parsePropertyAnnotation()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 25
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 5

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 25
ccs 22
cts 22
cp 1
rs 8.439
cc 5
eloc 19
nc 5
nop 2
crap 5
1
<?php
2
/**
3
 * This file is part of php-simple-request.
4
 *
5
 * php-simple-request is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Lesser General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * php-simple-request is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with php-simple-request.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
namespace Mcustiel\SimpleRequest;
19
20
use Doctrine\Common\Annotations\AnnotationReader;
21
use Mcustiel\SimpleRequest\Util\ValidatorBuilder;
22
use Mcustiel\SimpleRequest\Annotation\RequestAnnotation;
23
use Mcustiel\SimpleRequest\Annotation\ValidatorAnnotation;
24
use Mcustiel\SimpleRequest\Annotation\FilterAnnotation;
25
use Mcustiel\SimpleRequest\Util\FilterBuilder;
26
use Mcustiel\SimpleRequest\Annotation\ParseAs;
27
use Mcustiel\SimpleRequest\Annotation\AnnotationWithAssociatedClass;
28
use Mcustiel\SimpleRequest\Strategies\AnnotationParserFactory;
29
use Mcustiel\SimpleRequest\Strategies\PropertyParserBuilder;
30
use Mcustiel\SimpleRequest\Strategies\Properties\PropertyParser;
31
32
class ParserGenerator
33
{
34
    /**
35
     * @var \Doctrine\Common\Annotations\AnnotationReader
36
     */
37
    private $annotationReader;
38
    /**
39
     * @var \Mcustiel\SimpleRequest\Strategies\AnnotationParserFactory
40
     */
41
    private $annotationParserFactory;
42
43
    /**
44
     * @param \Doctrine\Common\Annotations\AnnotationReader $annotationReader
45
     *      External annotation reader instance (mostly for DI in tests). Created
46
     *      if not is set.
47
     */
48
    public function __construct(
49
        AnnotationReader $annotationReader,
50
        AnnotationParserFactory $annotationParserFactory
51
    ) {
52
        $this->annotationReader = $annotationReader;
53
        $this->annotationParserFactory = $annotationParserFactory;
54
    }
55
56
    public function addPropertyParser(PropertyParser $propertyParser)
57
    {
58
        $this->propertyParsers[] = $propertyParser;
0 ignored issues
show
Bug introduced by
The property propertyParsers does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
59
    }
60
61
    public function createRequestParser(
62
        $className,
63
        $parserObject,
64
        RequestBuilder $requestBuilder
65
    ) {
66
        $class = new \ReflectionClass($className);
67
        $parserObject->setRequestObject(new $className);
68
        foreach ($class->getProperties() as $property) {
69
            $propertyParserBuilder = new PropertyParserBuilder($property->getName());
70
            foreach ($this->annotationReader->getPropertyAnnotations($property) as $propertyAnnotation) {
71
                $this->annotationParserFactory
72
                    ->getAnnotationParserFor($propertyAnnotation)
73
                    ->execute($propertyAnnotation, $propertyParserBuilder);
74
            }
75
            $parserObject->addPropertyParser($propertyParserBuilder->build($requestBuilder));
76
        }
77
        return $parserObject;
78
    }
79
}
80