Passed
Pull Request — master (#116)
by Théo
02:21
created

RequirementsDumper   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 119
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A dumpCheckScript() 0 20 1
A retrieveFileShortName() 0 4 1
A dump() 0 16 2
A dumpRequirementsConfig() 0 14 1
A retrieveClassFileContents() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the box project.
7
 *
8
 * (c) Kevin Herrera <[email protected]>
9
 *     Théo Fidry <[email protected]>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14
15
namespace KevinGH\Box\RequirementChecker;
16
17
use Assert\Assertion;
18
use Composer\Semver\Semver;
19
use KevinGH\Box\Compactor\Json;
20
use ReflectionClass;
21
use const PHP_EOL;
22
use function array_column;
23
use function array_map;
24
use function implode;
25
use function KevinGH\Box\FileSystem\file_contents;
26
use function KevinGH\Box\FileSystem\filename;
27
use function sprintf;
28
use function str_replace;
29
use Symfony\Component\Console\Terminal;
30
use function var_export;
31
32
/**
33
 * @private
34
 */
35
final class RequirementsDumper
36
{
37
    public const CHECK_FILE_NAME = 'check_requirements.php';
38
39
    private const REQUIREMENTS_CHECKER_TEMPLATE = <<<'PHP'
40
<?php
41
42
/*
43
 * This file is part of the box project.
44
 *
45
 * (c) Kevin Herrera <[email protected]>
46
 *     Théo Fidry <[email protected]>
47
 *
48
 * This source file is subject to the MIT license that is bundled
49
 * with this source code in the file LICENSE.
50
 */
51
52
namespace KevinGH\Box\RequirementChecker;
53
54
//__AUTOLOAD__
55
56
use KevinGH\Box\RequirementChecker\Checker;
57
58
$checkPassed = Checker::checkRequirements();
59
60
if (false === $checkPassed) {
61
    exit(1);
62
}
63
64
PHP;
65
66
    private const REQUIREMENTS_CONFIG_TEMPLATE = <<<'PHP'
67
<?php
68
69
return __CONFIG__;
70
PHP;
71
72
    private const CLASSED_USED = [
73
        Checker::class,
74
        IO::class,
75
        Printer::class,
76
        Requirement::class,
77
        RequirementCollection::class,
78
        Terminal::class,
79
        Semver::class,
80
    ];
81
82
    /**
83
     * @return string[][]
84
     */
85
    public static function dump(string $composerJson): array
86
    {
87
        $filesWithContents = [
88
            self::dumpRequirementsConfig($composerJson),
89
        ];
90
91
        foreach (self::CLASSED_USED as $class) {
92
            $filesWithContents[] = [
93
                self::retrieveFileShortName($class),
94
                self::retrieveClassFileContents($class),
95
            ];
96
        }
97
98
        $filesWithContents[] = self::dumpCheckScript(...array_column($filesWithContents, 0));
99
100
        return $filesWithContents;
101
    }
102
103
    private static function dumpRequirementsConfig(string $composerJson): array
104
    {
105
        $config = AppRequirementsFactory::create(
106
            file_contents(
107
                str_replace('.json', '.lock', $composerJson)
108
            )
109
        );
110
111
        return [
112
            Checker::REQUIREMENTS_CONFIG,
113
            str_replace(
114
                '__CONFIG__',
115
                var_export($config, true),
116
                self::REQUIREMENTS_CONFIG_TEMPLATE
117
            ),
118
        ];
119
    }
120
121
    private static function dumpCheckScript(string ...$files): array
122
    {
123
        $autoloads = array_map(
124
            function (string $file): string {
125
                return sprintf(
126
                    'require_once __DIR__."/%s";',
127
                    $file
128
                );
129
            },
130
            $files
131
        );
132
133
        $autoloadStmt = implode(PHP_EOL, $autoloads);
134
135
        return [
136
            self::CHECK_FILE_NAME,
137
            str_replace(
138
                '//__AUTOLOAD__',
139
                $autoloadStmt,
140
                self::REQUIREMENTS_CHECKER_TEMPLATE
141
            ),
142
        ];
143
    }
144
145
    private static function retrieveClassFileContents(string $class): string
146
    {
147
        return file_contents((new ReflectionClass($class))->getFileName());
148
    }
149
150
    private static function retrieveFileShortName(string $class): string
151
    {
152
        return filename(
153
            (new ReflectionClass($class))->getFileName()
154
        );
155
    }
156
}
157