Completed
Push — master ( 2a503e...626ee6 )
by Kit Loong
01:12
created

DecimalField   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 14
lcom 0
cbo 2
dl 0
loc 68
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B makeField() 0 29 6
A getDecimalPrecision() 0 11 4
A getDoublePrecision() 0 8 3
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: liow.kitloong
5
 * Date: 2020/03/29
6
 * Time: 14:54
7
 */
8
9
namespace KitLoong\MigrationsGenerator\Generators;
10
11
use Doctrine\DBAL\Schema\Column;
12
use KitLoong\MigrationsGenerator\MigrationGeneratorSetting;
13
use KitLoong\MigrationsGenerator\MigrationMethod\ColumnModifier;
14
use KitLoong\MigrationsGenerator\Types\DBALTypes;
15
16
class DecimalField
17
{
18
    // (8, 2) are default value of decimal, float
19
    private const DEFAULT_DECIMAL_PRECISION = 8;
20
    private const DEFAULT_DECIMAL_SCALE = 2;
21
22
    // DBAL return (10, 0) if double length is empty
23
    private const EMPTY_PRECISION = 10;
24
    private const EMPTY_SCALE = 0;
25
26
    private $decorator;
27
28
    public function __construct(Decorator $decorator)
29
    {
30
        $this->decorator = $decorator;
31
    }
32
33
    public function makeField(array $field, Column $column): array
34
    {
35
        /** @var MigrationGeneratorSetting $setting */
36
        $setting = resolve(MigrationGeneratorSetting::class);
37
38
        switch ($setting->getPlatform()) {
39
            case Platform::POSTGRESQL:
40
                if ($field['type'] === DBALTypes::DECIMAL) {
41
                    $args = $this->getDecimalPrecision($column->getPrecision(), $column->getScale());
42
                }
43
                break;
44
            default:
45
                if (in_array($field['type'], [DBALTypes::DECIMAL, DBALTypes::FLOAT])) {
46
                    $args = $this->getDecimalPrecision($column->getPrecision(), $column->getScale());
47
                } else {
48
                    // double
49
                    $args = $this->getDoublePrecision($column->getPrecision(), $column->getScale());
50
                }
51
        }
52
53
        if (!empty($args)) {
54
            $field['args'] = $args;
55
        }
56
57
        if ($column->getUnsigned()) {
58
            $field['decorators'][] = ColumnModifier::UNSIGNED;
59
        }
60
        return $field;
61
    }
62
63
    private function getDecimalPrecision(int $precision, int $scale): array
64
    {
65
        $return = [];
66
        if ($precision != self::DEFAULT_DECIMAL_PRECISION || $scale != self::DEFAULT_DECIMAL_SCALE) {
67
            $return[] = $precision;
68
            if ($scale != self::DEFAULT_DECIMAL_SCALE) {
69
                $return[] = $scale;
70
            }
71
        }
72
        return $return;
73
    }
74
75
    private function getDoublePrecision(int $precision, int $scale)
76
    {
77
        if ($precision === self::EMPTY_PRECISION && $scale === self::EMPTY_SCALE) {
78
            return [];
79
        } else {
80
            return [$precision, $scale];
81
        }
82
    }
83
}
84