Passed
Pull Request — 3.x (#31)
by
unknown
13:18
created

TypeBuilder::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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