Issues (3)

src/CodeGeneration/YamlDefinitionLoader.php (1 issue)

Severity
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 6
    public function load(string $filename, DefinitionGroup $definitionGroup = null): DefinitionGroup
24
    {
25 6
        $fileContents = file_get_contents($filename);
26
27 6
        if ( ! is_string($fileContents) || empty($fileContents)) {
0 ignored issues
show
The condition is_string($fileContents) is always true.
Loading history...
28 1
            throw new InvalidArgumentException("File {$filename} does not contain anything");
29
        }
30
31 5
        $definition = Yaml::parse($fileContents);
32
33 5
        if ( ! is_array($definition)) {
34 1
            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
                $type = $fieldDefinition['type'] ?? $definitionGroup->typeForField($fieldName);
81 3
                $command->field($fieldName, TypeNormalizer::normalize($type), $fieldDefinition['example'] ?? null);
82
83 3
                if (isset($fieldDefinition['serializer'])) {
84 1
                    $command->fieldSerializer($fieldName, $fieldDefinition['serializer']);
85
                }
86
87 3
                if (isset($fieldDefinition['deserializer'])) {
88 1
                    $command->fieldDeserializer($fieldName, $fieldDefinition['deserializer']);
89
                }
90
            }
91
        }
92 4
    }
93
94 4
    private function loadEvents(DefinitionGroup $definitionGroup, array $events)
95
    {
96 4
        foreach ($events as $eventName => $eventDefinition) {
97 3
            $event = $definitionGroup->event($eventName);
98 3
            $event->withFieldsFrom($eventDefinition['fields_from'] ?? '');
99 3
            $fields = $eventDefinition['fields'] ?? [];
100
101 3
            foreach ($fields as $fieldName => $fieldDefinition) {
102 3
                if (is_string($fieldDefinition)) {
103 1
                    $fieldDefinition = ['type' => TypeNormalizer::normalize($fieldDefinition)];
104
                }
105
106 3
                $type = $fieldDefinition['type'] ?? $definitionGroup->typeForField($fieldName);
107 3
                $event->field($fieldName, TypeNormalizer::normalize($type), (string) ($fieldDefinition['example'] ?? null));
108
109 3
                if (isset($fieldDefinition['serializer'])) {
110 1
                    $event->fieldSerializer($fieldName, $fieldDefinition['serializer']);
111
                }
112
113 3
                if (isset($fieldDefinition['deserializer'])) {
114 1
                    $event->fieldDeserializer($fieldName, $fieldDefinition['deserializer']);
115
                }
116
            }
117
        }
118 4
    }
119
120 4
    private function loadFieldDefaults(DefinitionGroup $definitionGroup, array $defaults)
121
    {
122 4
        foreach ($defaults as $field => $default) {
123 1
            $definitionGroup->fieldDefault($field, $default['type'], $default['example'] ?? null);
124
125 1
            if (isset($default['serializer'])) {
126 1
                $definitionGroup->fieldSerializer($field, $default['serializer']);
127
            }
128
129 1
            if (isset($default['deserializer'])) {
130 1
                $definitionGroup->fieldDeserializer($field, $default['deserializer']);
131
            }
132
        }
133 4
    }
134
}
135