Passed
Pull Request — 2.x (#21)
by Alexander
29:47 queued 14:03
created

ChangesCountGenerator::generate()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 2
b 0
f 0
nc 4
nop 1
dl 0
loc 16
rs 9.9332
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\Schema\Generator\Migrations;
6
7
use Cycle\Migrations\Atomizer\Atomizer;
8
use Cycle\Schema\Generator\Migrations\Changes\ChangeType;
9
use Cycle\Schema\Generator\Migrations\Changes\Collector;
10
11
final class ChangesCountGenerator implements NameGeneratorInterface
12
{
13
    public function generate(Atomizer $atomizer): string
14
    {
15
        $collector = new Collector();
16
        $map = [];
17
        foreach ($collector->collect($atomizer) as $pair) {
18
            $key = $this->changeToString($pair[0]);
19
            $map[$key] ??= 0;
20
            $map[$key]++;
21
        }
22
23
        $result = [];
24
        foreach ($map as $key => $cnt) {
25
            $result[] = "{$key}{$cnt}";
26
        }
27
28
        return \implode('_', $result);
29
    }
30
31
    private function changeToString(ChangeType $change): string
32
    {
33
        return match ($change) {
34
            ChangeType::CreateTable => 'tc',
35
            ChangeType::DropTable => 'td',
36
            ChangeType::RenameTable => 'tr',
37
            ChangeType::ChangeTable => 'tc',
38
            ChangeType::AddColumn => 'ca',
39
            ChangeType::DropColumn => 'cd',
40
            ChangeType::AlterColumn => 'cl',
41
            ChangeType::AddIndex => 'ia',
42
            ChangeType::DropIndex => 'id',
43
            ChangeType::AlterIndex => 'il',
44
            ChangeType::AddFk => 'fa',
45
            ChangeType::DropFk => 'fd',
46
            ChangeType::AlterFk => 'fl',
47
        };
48
    }
49
}
50