Passed
Pull Request — 3.x (#31)
by
unknown
14:21
created

TypeBuilder   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 19
c 1
b 0
f 0
dl 0
loc 35
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A create() 0 13 1
1
<?php
2
3
4
declare(strict_types=1);
5
6
namespace Cycle\Migrations\V2;
7
8
use Cycle\Database\DatabaseInterface;
9
10
class TypeBuilder
11
{
12
    public const ENUM = 'ENUM';
13
    public const RANGE = 'RANGE';
14
15
    private DatabaseInterface $db;
16
    private string $name;
17
    private array $values;
18
    private string $type;
19
20
    public function __construct(
21
        string $name,
22
        array $values,
23
        string $type,
24
        DatabaseInterface $db
25
    ) {
26
        $this->db = $db;
27
        $this->name = $name;
28
        $this->values = $values;
29
        $this->type = $type;
30
    }
31
32
    public function create(): void
33
    {
34
        $values = implode(',', array_map(static fn($v) => "'{$v}'", $this->values));
35
36
        $query = sprintf(
37
            'CREATE TYPE %s AS %s (%s);',
38
            $this->name,
39
            $this->type,
40
            $values
41
        );
42
43
        $this->db->execute($query);
44
        $this->db->commit();
45
    }
46
}
47