HydratorContextFactory::build()   A
last analyzed

Complexity

Conditions 6
Paths 10

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6.0359

Importance

Changes 0
Metric Value
cc 6
eloc 16
nc 10
nop 1
dl 0
loc 23
ccs 9
cts 10
cp 0.9
crap 6.0359
rs 9.1111
c 0
b 0
f 0
1
<?php
2
3
namespace Sf4\Populator;
4
5
use Doctrine\Common\Annotations\AnnotationReader;
6
7
class HydratorContextFactory implements HydratorContextFactoryInterface
8
{
9
    /**
10
     * @param $class
11
     * @return HydratorContext
12 3
     * @throws \ReflectionException
13
     * @throws \Doctrine\Common\Annotations\AnnotationException
14 3
     */
15 3
    public function build($class)
16
    {
17 3
        if (is_object($class)) {
18
            $class = get_class($class);
19
        }
20 3
        if (!class_exists($class)) {
21 3
            throw new \InvalidArgumentException(sprintf("Class %s does not exist", $class));
22 3
        }
23 3
        $context = new HydratorContext;
24 3
        $context->setClass($class);
25 3
        $reflection = new \ReflectionClass($class);
26
        $annotationReader = new AnnotationReader();
27 3
        foreach ($reflection->getProperties() as $property) {
28
            $annotations = $annotationReader->getPropertyAnnotations($property);
29
            $metadata = new PropertyMetadata($property->getName());
30
            foreach($annotations as $annotation) {
31
                if ($annotation instanceof PopulatorAnnotation) {
32
                    $annotation->process($metadata, $this);
33
                }
34
            }
35
            $context->addProperty($metadata);
36
        }
37
        return $context;
38
    }
39
}
40