Completed
Push — feature/collation ( 3fa00f )
by Kit Loong
04:36
created

CollationModifier::generate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 16
rs 9.9666
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: liow.kitloong
5
 * Date: 2020/11/22
6
 */
7
8
namespace KitLoong\MigrationsGenerator\Generators\Modifier;
9
10
use Doctrine\DBAL\Schema\Column;
11
use KitLoong\MigrationsGenerator\Generators\Decorator;
12
use KitLoong\MigrationsGenerator\MigrationMethod\ColumnModifier;
13
use KitLoong\MigrationsGenerator\MigrationsGeneratorSetting;
14
15
class CollationModifier
16
{
17
    private $decorator;
18
19
    public function __construct(Decorator $decorator)
20
    {
21
        $this->decorator = $decorator;
22
    }
23
24
    public function generate(string $tableName, Column $column): string
25
    {
26
        $setting = app(MigrationsGeneratorSetting::class);
27
        $tableCollation = $setting->getSchema()->listTableDetails($tableName)->getOptions()['collation'] ?? null;
28
29
        $columnCollation = $column->getPlatformOptions()['collation'] ?? null;
30
        if (!empty($column->getPlatformOptions()['collation'])) {
31
            if ($columnCollation !== $tableCollation) {
32
                return $this->decorator->decorate(
33
                    ColumnModifier::COLLATION,
34
                    [$this->decorator->columnDefaultToString($columnCollation)]
35
                );
36
            }
37
        }
38
39
        return '';
40
    }
41
}
42