Completed
Pull Request — 4.x (#12)
by Kit Loong
04:42
created

GeometryField::makeField()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.0092

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 20
ccs 11
cts 12
cp 0.9167
crap 4.0092
rs 9.8666
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: liow.kitloong
5
 * Date: 2020/04/07
6
 */
7
8
namespace KitLoong\MigrationsGenerator\Generators;
9
10
use KitLoong\MigrationsGenerator\MigrationsGeneratorSetting;
11
use KitLoong\MigrationsGenerator\MigrationMethod\PgSQLGeography;
12
use KitLoong\MigrationsGenerator\Repositories\PgSQLRepository;
13
14
class GeometryField
15
{
16
    private $pgsqlRepository;
17
18 60
    public function __construct(PgSQLRepository $pgSQLRepository)
19
    {
20 60
        $this->pgsqlRepository = $pgSQLRepository;
21 60
    }
22
23 6
    public function makeField(string $tableName, array $field)
24
    {
25
        /** @var MigrationsGeneratorSetting $setting */
26 6
        $setting = app(MigrationsGeneratorSetting::class);
27
28 6
        switch ($setting->getPlatform()) {
29
            case Platform::POSTGRESQL:
30 6
                $columnType = $this->pgsqlRepository->getTypeByColumnName($tableName, $field['field']);
31 6
                if ($columnType !== null) {
32 6
                    $type = strtolower($columnType);
33 6
                    $type = preg_replace('/\s+/', '', $type);
34
35 6
                    if (isset(PgSQLGeography::MAP[$type])) {
36 6
                        $field['type'] = PgSQLGeography::MAP[$type];
37
                    }
38
                }
39 6
                break;
40
            default:
41
        }
42 6
        return $field;
43
    }
44
}
45