ObjectFactory   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 3
dl 0
loc 40
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 16 3
A supports() 0 4 1
instantiate() 0 1 ?
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of the KleijnWeb\PhpApi\Hydrator package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace KleijnWeb\PhpApi\Hydrator\Processors\Factory;
10
11
use KleijnWeb\PhpApi\Descriptions\Description\Schema\ObjectSchema;
12
use KleijnWeb\PhpApi\Descriptions\Description\Schema\Schema;
13
use KleijnWeb\PhpApi\Hydrator\ProcessorBuilder;
14
use KleijnWeb\PhpApi\Hydrator\Processors\Object\ObjectProcessor;
15
16
/**
17
 * @author John Kleijn <[email protected]>
18
 */
19
abstract class ObjectFactory implements Factory
20
{
21
    /**
22
     * @param Schema           $schema
23
     * @param ProcessorBuilder $builder
24
     * @return ObjectProcessor|null
25
     */
26
    public function create(Schema $schema, ProcessorBuilder $builder)
27
    {
28
        if (!$this->supports($schema)) {
29
            return null;
30
        }
31
32
        /** @var ObjectSchema $objectSchema */
33
        $objectSchema = $schema;
34
        $processor = $this->instantiate($objectSchema, $builder);
35
36
        foreach ($objectSchema->getPropertySchemas() as $propertyName => $propertySchema) {
0 ignored issues
show
Bug introduced by
The expression $objectSchema->getPropertySchemas() of type object<stdClass> is not traversable.
Loading history...
37
            $processor->setPropertyProcessor($propertyName, $builder->build($propertySchema));
38
        }
39
40
        return $processor;
41
    }
42
43
    /**
44
     * @param Schema $schema
45
     * @return bool
46
     */
47
    public function supports(Schema $schema)
48
    {
49
        return $schema instanceof ObjectSchema;
50
    }
51
52
    /**
53
     * @param ObjectSchema     $schema
54
     * @param ProcessorBuilder $builder
55
     * @return ObjectProcessor
56
     */
57
    abstract protected function instantiate(ObjectSchema $schema, ProcessorBuilder $builder): ObjectProcessor;
58
}
59