Completed
Push — 4.x ( f77772...da046c )
by Kit Loong
10:27
created

DefaultModifier::generate()   C

Complexity

Conditions 12
Paths 12

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 6.9666
c 0
b 0
f 0
cc 12
nc 12
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: liow.kitloong
5
 * Date: 2020/03/31
6
 * Time: 18:41
7
 */
8
9
namespace KitLoong\MigrationsGenerator\Generators\Modifier;
10
11
use Doctrine\DBAL\Schema\Column;
12
use KitLoong\MigrationsGenerator\Generators\BooleanField;
13
use KitLoong\MigrationsGenerator\Generators\DatetimeField;
14
use KitLoong\MigrationsGenerator\Generators\Decorator;
15
use KitLoong\MigrationsGenerator\MigrationMethod\ColumnModifier;
16
use KitLoong\MigrationsGenerator\Types\DBALTypes;
17
18
class DefaultModifier
19
{
20
    private $booleanField;
21
    private $datetimeField;
22
    private $decorator;
23
24
    public function __construct(BooleanField $booleanField, DatetimeField $datetimeField, Decorator $decorator)
25
    {
26
        $this->booleanField = $booleanField;
27
        $this->datetimeField = $datetimeField;
28
        $this->decorator = $decorator;
29
    }
30
31
    /**
32
     * @param  string  $dbalType
33
     * @param  Column  $column
34
     * @return string
35
     */
36
    public function generate(string $dbalType, Column $column): string
37
    {
38
        switch ($dbalType) {
39
            case DBALTypes::SMALLINT:
40
            case DBALTypes::INTEGER:
41
            case DBALTypes::BIGINT:
42
            case DBALTypes::MEDIUMINT:
43
            case DBALTypes::TINYINT:
44
            case DBALTypes::DECIMAL:
45
            case DBALTypes::FLOAT:
46
            case DBALTypes::DOUBLE:
47
                $default = $column->getDefault();
48
                break;
49
            case DBALTypes::BOOLEAN:
50
                $default = $this->booleanField->makeDefault($column);
51
                break;
52
            case DBALTypes::DATETIME_MUTABLE:
53
            case DBALTypes::TIMESTAMP:
54
                return $this->datetimeField->makeDefault($column);
55
            default:
56
                $default = $this->decorator->columnDefaultToString($column->getDefault());
57
        }
58
59
        return $this->decorator->decorate(ColumnModifier::DEFAULT, [$default]);
60
    }
61
}
62