Completed
Push — master ( d1d2ef...0e04b4 )
by Kit Loong
04:05
created

DecimalField::makeField()   A

Complexity

Conditions 6
Paths 16

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6

Importance

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