Completed
Branch 4.x (7a7a18)
by Kit Loong
03:20
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
 * 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 54
    public function __construct(Decorator $decorator)
29
    {
30 54
        $this->decorator = $decorator;
31 54
    }
32
33 18
    public function makeField(array $field, Column $column): array
34
    {
35
        /** @var MigrationGeneratorSetting $setting */
36 18
        $setting = app(MigrationGeneratorSetting::class);
37
38 18
        switch ($setting->getPlatform()) {
39 6
            case Platform::POSTGRESQL:
40 6
                if ($field['type'] === DBALTypes::DECIMAL) {
41 3
                    $args = $this->getDecimalPrecision($column->getPrecision(), $column->getScale());
42
                }
43 6
                break;
44
            default:
45 12
                if (in_array($field['type'], [DBALTypes::DECIMAL, DBALTypes::FLOAT])) {
46 9
                    $args = $this->getDecimalPrecision($column->getPrecision(), $column->getScale());
47
                } else {
48
                    // double
49 3
                    $args = $this->getDoublePrecision($column->getPrecision(), $column->getScale());
50
                }
51
        }
52
53 18
        if (!empty($args)) {
54 9
            $field['args'] = $args;
55
        }
56
57 18
        if ($column->getUnsigned()) {
58 6
            $field['decorators'][] = ColumnModifier::UNSIGNED;
59
        }
60 18
        return $field;
61
    }
62
63 12
    private function getDecimalPrecision(int $precision, int $scale): array
64
    {
65 12
        $return = [];
66 12
        if ($precision != self::DEFAULT_DECIMAL_PRECISION || $scale != self::DEFAULT_DECIMAL_SCALE) {
67 9
            $return[] = $precision;
68 9
            if ($scale != self::DEFAULT_DECIMAL_SCALE) {
69 6
                $return[] = $scale;
70
            }
71
        }
72 12
        return $return;
73
    }
74
75 3
    private function getDoublePrecision(int $precision, int $scale)
76
    {
77 3
        if ($precision === self::EMPTY_PRECISION && $scale === self::EMPTY_SCALE) {
78 3
            return [];
79
        } else {
80
            return [$precision, $scale];
81
        }
82
    }
83
}
84