Passed
Push — master ( 0c6c2f...4ea690 )
by Frank
02:07
created

YamlDefinitionLoader::loadTypeHandlers()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 7
nc 9
nop 2
dl 0
loc 13
ccs 8
cts 8
cp 1
crap 5
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EventSauce\EventSourcing\CodeGeneration;
6
7
use InvalidArgumentException;
8
use function is_array;
9
use Symfony\Component\Yaml\Yaml;
10
use const PATHINFO_EXTENSION;
11
use function file_get_contents;
12
use function in_array;
13
use function is_string;
14
use function pathinfo;
15
16
class YamlDefinitionLoader implements DefinitionLoader
17
{
18 1
    public function canLoad(string $filename): bool
19
    {
20 1
        return in_array(pathinfo($filename, PATHINFO_EXTENSION), ['yaml', 'yml']);
21
    }
22
23 4
    public function load(string $filename, DefinitionGroup $definitionGroup = null): DefinitionGroup
24
    {
25 4
        $fileContents = file_get_contents($filename);
26
27 4
        if ( ! is_string($fileContents)) {
0 ignored issues
show
introduced by
The condition is_string($fileContents) is always true.
Loading history...
28
            throw new InvalidArgumentException("File {$filename} does not contain anything");
29
        }
30
31 4
        $definition = Yaml::parse($fileContents);
32
33 4
        if ( ! is_array($definition)) {
34
            throw new InvalidArgumentException('The definition is incorrectly formatted');
35
        }
36
37 4
        $definitionGroup = $definitionGroup ?: new DefinitionGroup();
38
39 4
        if (isset($definition['namespace'])) {
40 3
            $definitionGroup->withNamespace($definition['namespace']);
41
        }
42
43 4
        $this->loadTypeHandlers($definitionGroup, $definition['types'] ?? []);
44 4
        $this->loadFieldDefaults($definitionGroup, $definition['fields'] ?? []);
45 4
        $this->loadCommands($definitionGroup, $definition['commands'] ?? []);
46 4
        $this->loadEvents($definitionGroup, $definition['events'] ?? []);
47
48 4
        return $definitionGroup;
49
    }
50
51 4
    private function loadTypeHandlers(DefinitionGroup $definitionGroup, array $types)
52
    {
53 4
        foreach ($types as $type => $handlers) {
54 1
            if (isset($handlers['type'])) {
55 1
                $definitionGroup->aliasType($type, $handlers['type']);
56
            }
57
58 1
            if (isset($handlers['serializer'])) {
59 1
                $definitionGroup->typeSerializer($type, $handlers['serializer']);
60
            }
61
62 1
            if (isset($handlers['deserializer'])) {
63 1
                $definitionGroup->typeDeserializer($type, $handlers['deserializer']);
64
            }
65
        }
66 4
    }
67
68 4
    private function loadCommands(DefinitionGroup $definitionGroup, array $commands)
69
    {
70 4
        foreach ($commands as $commandName => $commandDefinition) {
71 4
            $fields = $commandDefinition['fields'] ?? [];
72 4
            $command = $definitionGroup->command($commandName);
73 4
            $command->withFieldsFrom($commandDefinition['fields_from'] ?? '');
74
75 4
            foreach ($fields as $fieldName => $fieldDefinition) {
76 3
                if (is_string($fieldDefinition)) {
77 1
                    $fieldDefinition = ['type' => $fieldDefinition];
78
                }
79
80 3
                $command->field($fieldName, $fieldDefinition['type'], $fieldDefinition['example'] ?? null);
81
82 3
                if (isset($fieldDefinition['serializer'])) {
83 1
                    $command->fieldSerializer($fieldName, $fieldDefinition['serializer']);
84
                }
85
86 3
                if (isset($fieldDefinition['deserializer'])) {
87 1
                    $command->fieldDeserializer($fieldName, $fieldDefinition['deserializer']);
88
                }
89
            }
90
        }
91 4
    }
92
93 4
    private function loadEvents(DefinitionGroup $definitionGroup, array $events)
94
    {
95 4
        foreach ($events as $eventName => $eventDefinition) {
96 3
            $event = $definitionGroup->event($eventName);
97 3
            $event->withFieldsFrom($eventDefinition['fields_from'] ?? '');
98 3
            $fields = $eventDefinition['fields'] ?? [];
99
100 3
            foreach ($fields as $fieldName => $fieldDefinition) {
101 3
                if (is_string($fieldDefinition)) {
102 1
                    $fieldDefinition = ['type' => TypeNormalizer::normalize($fieldDefinition)];
103
                }
104
105 3
                $event->field($fieldName, TypeNormalizer::normalize($fieldDefinition['type']), (string) ($fieldDefinition['example'] ?? null));
106
107 3
                if (isset($fieldDefinition['serializer'])) {
108 1
                    $event->fieldSerializer($fieldName, $fieldDefinition['serializer']);
109
                }
110
111 3
                if (isset($fieldDefinition['deserializer'])) {
112 1
                    $event->fieldDeserializer($fieldName, $fieldDefinition['deserializer']);
113
                }
114
            }
115
        }
116 4
    }
117
118 4
    private function loadFieldDefaults(DefinitionGroup $definitionGroup, array $defaults)
119
    {
120 4
        foreach ($defaults as $field => $default) {
121 1
            $definitionGroup->fieldDefault($field, $default['type'], $default['example'] ?? null);
122
123 1
            if (isset($default['serializer'])) {
124 1
                $definitionGroup->fieldSerializer($field, $default['serializer']);
125
            }
126
127 1
            if (isset($default['deserializer'])) {
128 1
                $definitionGroup->fieldDeserializer($field, $default['deserializer']);
129
            }
130
        }
131 4
    }
132
}
133