Completed
Push — master ( f5a72a...e5266d )
by Frank
06:35
created

DefinitionGroup::namespace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EventSauce\EventSourcing\CodeGeneration;
6
7
use function array_key_exists;
8
use EventSauce\EventSourcing\PointInTime;
9
use function var_dump;
10
11
final class DefinitionGroup
12
{
13
    /**
14
     * @var string
15
     */
16
    private $namespace;
17
18
    /**
19
     * @var EventDefinition[]
20
     */
21
    private $events = [];
22
23
    /**
24
     * @var array
25
     */
26
    private $defaults = [];
27
28
    /**
29
     * @var array
30
     */
31
    private $typeSerializer = [
32
        'string'  => '({type}) {param}',
33
        'array'   => '({type}) {param}',
34
        'integer' => '({type}) {param}',
35
        'int'     => '({type}) {param}',
36
        'bool'    => '({type}) {param}',
37
        'float'   => '({type}) {param}',
38
    ];
39
40
    /**
41
     * @var array
42
     */
43
    private $typeDeserializer = [
44
        'string'  => '({type}) {param}',
45
        'array'   => '({type}) {param}',
46
        'integer' => '({type}) {param}',
47
        'int'     => '({type}) {param}',
48
        'bool'    => '({type}) {param}',
49
        'float'   => '({type}) {param}',
50
    ];
51
52
    /**
53
     * @var array
54
     */
55
    private $fieldSerializer = [];
56
57
    /**
58
     * @var array
59
     */
60
    private $fieldDeserializer = [];
61
62
    /**
63
     * @var CommandDefinition[]
64
     */
65
    private $commands = [];
66
67
    /**
68
     * @var string[]
69
     */
70
    private $typeAliases = [];
71
72 4
    public function __construct()
73
    {
74 4
        $this->typeSerializer(PointInTime::class, '{param}->toString()');
75 4
        $this->typeDeserializer(PointInTime::class, '{type}::fromString({param})');
76 4
    }
77
78
    public static function create(string $namespace): DefinitionGroup
79
    {
80
        return (new DefinitionGroup())->withNamespace($namespace);
81
    }
82
83 3
    public function withNamespace(string $namespace): DefinitionGroup
84
    {
85 3
        $this->namespace = $namespace;
86
87 3
        return $this;
88
    }
89
90 4
    public function typeSerializer(string $type, string $template)
91
    {
92 4
        $type = $this->resolveTypeAlias($type);
93
94 4
        $this->typeSerializer[TypeNormalizer::normalize($type)] = $template;
95 4
    }
96
97 7
    public function serializerForType($type)
98
    {
99 7
        $type = $this->resolveTypeAlias($type);
100
101 7
        return $this->typeSerializer[$type] ?? 'new {type}({param})';
102
    }
103
104 4
    public function typeDeserializer(string $type, string $template)
105
    {
106 4
        $type = $this->resolveTypeAlias($type);
107
108 4
        $this->typeDeserializer[TypeNormalizer::normalize($type)] = $template;
109 4
    }
110
111 7
    public function deserializerForType($type)
112
    {
113 7
        $type = $this->resolveTypeAlias($type);
114
115 7
        return $this->typeDeserializer[$type] ?? 'new {type}({param})';
116
    }
117
118 1
    public function fieldSerializer(string $field, string $template)
119
    {
120 1
        $this->fieldSerializer[$field] = $template;
121 1
    }
122
123 8
    public function serializerForField($field)
124
    {
125 8
        return $this->fieldSerializer[$field] ?? null;
126
    }
127
128 1
    public function fieldDeserializer(string $field, string $template)
129
    {
130 1
        $this->fieldDeserializer[$field] = $template;
131 1
    }
132
133 8
    public function deserializerForField($field)
134
    {
135 8
        return $this->fieldDeserializer[$field] ?? null;
136
    }
137
138 1
    public function fieldDefault(string $name, string $type, string $example = null)
139
    {
140 1
        $type = $this->resolveTypeAlias($type);
141 1
        $this->defaults[$name] = compact('type', 'example');
142 1
    }
143
144 1
    public function aliasType(string $alias, string $type)
145
    {
146 1
        $this->typeAliases[$alias] = TypeNormalizer::normalize($type);
147 1
    }
148
149 11
    public function resolveTypeAlias(string $alias = null)
150
    {
151 11
        while (isset($this->typeAliases[$alias])) {
152 1
            $alias = $this->typeAliases[$alias];
153
        }
154
155 11
        return $alias;
156
    }
157
158 3
    public function event(string $name): EventDefinition
159
    {
160 3
        return $this->events[] = new EventDefinition($this, $name);
161
    }
162
163 4
    public function command(string $name)
164
    {
165 4
        return $this->commands[] = new CommandDefinition($this, $name);
166
    }
167
168
    public function typeForField(string $field): string
169
    {
170
        return $this->defaults[$field]['type'] ?? 'string';
171
    }
172
173 1
    public function exampleForField(string $field)
174
    {
175 1
        return $this->defaults[$field]['example'] ?? null;
176
    }
177
178
    /**
179
     * @return EventDefinition[]
180
     */
181 12
    public function events(): array
182
    {
183 12
        return $this->events;
184
    }
185
186
    /**
187
     * @return CommandDefinition[]
188
     */
189 12
    public function commands(): array
190
    {
191 12
        return $this->commands;
192
    }
193
194 11
    public function namespace(): string
195
    {
196 11
        return $this->namespace;
197
    }
198
}
199