ArrayDriver::loadMetadataForClass()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 44
Code Lines 30

Duplication

Lines 6
Ratio 13.64 %

Importance

Changes 0
Metric Value
dl 6
loc 44
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 30
nc 4
nop 1
1
<?php
2
3
namespace Psi\Component\ContentType\Metadata\Driver;
4
5
use Metadata\Driver\AdvancedDriverInterface;
6
use Psi\Component\ContentType\Metadata\ClassMetadata;
7
use Psi\Component\ContentType\Metadata\PropertyMetadata;
8
9
class ArrayDriver implements AdvancedDriverInterface
10
{
11
    /**
12
     * @var array
13
     */
14
    private $config;
15
16
    /**
17
     * @param array $config
18
     */
19
    public function __construct(array $config)
20
    {
21
        $this->config = $config;
22
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function loadMetadataForClass(\ReflectionClass $class)
28
    {
29
        if (!isset($this->config[$class->getName()])) {
30
            return;
31
        }
32
33
        $config = array_merge([
34
            'fields' => [],
35
        ], $this->config[$class->getName()]);
36
37
        $classMetadata = new ClassMetadata($class->getName());
38
        $defaults = [
39
            'type' => null,
40
            'role' => null,
41
            'group' => null,
42
            'shared' => [],
43
            'form' => [],
44
            'view' => [],
45
            'storage' => [],
46
        ];
47
48
        foreach ($config['fields'] as $fieldName => $fieldConfig) {
49 View Code Duplication
            if ($diff = array_diff(array_keys($fieldConfig), array_keys($defaults))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
                throw new \InvalidArgumentException(sprintf(
51
                    'Invalid configuration key(s) "%s" for field "%s" on class "%s", valid keys: "%s"',
52
                    implode('", "', $diff), $fieldName, $class->getName(), implode('", "', array_keys($defaults))
53
                ));
54
            }
55
56
            $fieldConfig = array_merge($defaults, $fieldConfig);
57
            $propertyMetadata = new PropertyMetadata(
58
                $class->getName(),
59
                $fieldName,
60
                $fieldConfig['type'],
61
                $fieldConfig['role'],
62
                $fieldConfig['group'],
63
                array_intersect_key($fieldConfig, array_flip(['view', 'storage', 'form', 'shared']))
64
            );
65
66
            $classMetadata->addPropertyMetadata($propertyMetadata);
67
        }
68
69
        return $classMetadata;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function getAllClassNames()
76
    {
77
        return array_keys($this->config);
78
    }
79
}
80