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

CollationModifier   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 25
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A generate() 0 16 3
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