Passed
Push — 6.0 ( 4ac4e1...87e1d7 )
by Olivier
01:56
created

GenerateTerritoryData   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 20
dl 0
loc 57
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 17 1
A __construct() 0 4 1
A render() 0 27 1
1
<?php
2
3
namespace ICanBoogie\CLDR\Generator\Command;
4
5
use ICanBoogie\CLDR\Repository;
6
use Symfony\Component\Console\Attribute\AsCommand;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\VarExporter\VarExporter;
11
use function ICanBoogie\CLDR\Generator\indent;
12
13
#[AsCommand(self::GENERATED_FILE)]
14
final class GenerateTerritoryData extends Command
15
{
16
    private const GENERATED_FILE = 'src/Supplemental/Territory/TerritoryData.php';
17
18
    public function __construct(
19
        private readonly Repository $repository
20
    ) {
21
        parent::__construct();
22
    }
23
24
    protected function execute(InputInterface $input, OutputInterface $output): int
25
    {
26
        /**
27
         * @var string[] $codes
28
         *
29
         * @link https://github.com/unicode-org/cldr-json/blob/45.0.0/cldr-json/cldr-localenames-full/main/en-001/territories.json
30
         */
31
        $codes = array_keys($this->repository->locale_for('en-001')['territories']);
32
        $codes = array_values(array_filter($codes, fn($code) => !str_contains($code, '-alt')));
33
34
        $contents = $this->render(
35
            codes: indent(VarExporter::export($codes), 2),
36
        );
37
38
        file_put_contents(self::GENERATED_FILE, $contents);
39
40
        return self::SUCCESS;
41
    }
42
43
    private function render(
44
        string $codes,
45
    ): string {
46
        $class = __CLASS__;
47
48
        return <<<PHP
49
        <?php
50
51
        /**
52
         * CODE GENERATED; DO NOT EDIT.
53
         *
54
         * {@see \\$class}
55
         */
56
57
        namespace ICanBoogie\CLDR\Supplemental\Territory;
58
59
        /**
60
         * @internal
61
         * @codeCoverageIgnore
62
         */
63
        final class TerritoryData
64
        {
65
            /**
66
             * @link https://github.com/unicode-org/cldr-json/blob/45.0.0/cldr-json/cldr-localenames-full/main/en-001/territories.json
67
             */
68
            public const CODES =
69
        $codes;
70
71
            private function __construct()
72
            {
73
            }
74
        }
75
76
        PHP;
77
    }
78
}
79