Passed
Pull Request — 3.x (#31)
by
unknown
13:18
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
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