Completed
Push — master ( 5d18c7...cabcf3 )
by Hannes
16s queued 13s
created

build_formats.php ➔ render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 2
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
use byrokrat\banking\UndefinedAccount;
6
7
require __DIR__ . '/../vendor/autoload.php';
8
9
$defs = json_decode(file_get_contents(__DIR__ . '/formats.json'), true);
10
11
$createdFormats = [];
12
13
// Create formats from definitions
14
15
foreach ($defs['formats'] as $data) {
16
    $data['specification-date'] = $defs['specification-date'];
17
    createFormat($data, $createdFormats);
18
}
19
20
// Validate that there are no clearing number collisions
21
22
echo "Validating that there are no clearing number collisions > ";
23
foreach (range(1000, 9999) as $clearing) {
24
    $formatFound = false;
25
    $account = new UndefinedAccount('', (string)$clearing, '', '' ,'');
26
    foreach ($createdFormats as $format) {
27
        if ($format->isValidClearing($account)) {
28
            if ($formatFound) {
29
                throw new LogicException("Multiple formats matching clearing $clearing");
30
            }
31
            $formatFound = true;
32
        }
33
    }
34
}
35
echo "DONE\n";
36
37
// Generate UnknownFormat
38
39
$defs['UnknownFormat']['specification-date'] = $defs['specification-date'];
40
createFormat($defs['UnknownFormat'], $createdFormats);
41
42
// Generate FormatFactory
43
44
file_put_contents(
45
    __DIR__ . "/../src/Format/FormatFactory.php",
46
    render('factoryTemplate', [
47
        'formats' => array_keys($createdFormats),
48
        'specification-date' => $defs['specification-date']
49
    ])
50
);
51
echo "Generated FormatFactory\n";
52
53
// Helper methods
54
55
function render(string $template, array $data): string
56
{
57
    ob_start();
58
    include __DIR__ . "/$template.php";
59
    $content = ob_get_contents();
60
    ob_end_clean();
61
    return "<?php\n" . $content;
62
}
63
64
function createFormat(array $data, array &$createdFormats)
65
{
66
    // Check that name is not defined twice
67
    if (isset($createdFormats[$data['classname']])) {
68
        throw new LogicException("Multiple formats named {$data['classname']}");
69
    }
70
71
    // Generate class
72
    file_put_contents(
73
        __DIR__ . "/../src/Format/Build/{$data['classname']}.php",
74
        render('template', $data)
75
    );
76
77
    $classname = "byrokrat\banking\Format\Build\\{$data['classname']}";
78
79
    $createdFormats[$data['classname']] = new $classname;
80
81
    // Generate test
82
    file_put_contents(
83
        __DIR__ . "/../tests/Format/Build/{$data['classname']}Test.php",
84
        render('testTemplate', $data)
85
    );
86
87
    echo "Generated {$data['classname']}\n";
88
}
89