Passed
Push — master ( 10a192...087136 )
by Aleksei
07:38 queued 05:42
created

PrimaryKeys::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license MIT
7
 * @author  Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Cycle\Migrations\Operation\Table;
13
14
use Cycle\Migrations\CapsuleInterface;
15
use Cycle\Migrations\Exception\Operation\TableException;
16
use Cycle\Migrations\Operation\AbstractOperation;
17
18
final class PrimaryKeys extends AbstractOperation
19
{
20
    /** @var array */
21
    private $columns = [];
22
23
    /**
24
     * @param string $table
25
     * @param array  $columns
26
     */
27
    public function __construct(string $table, array $columns)
28
    {
29
        parent::__construct($table);
30
        $this->columns = $columns;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function execute(CapsuleInterface $capsule): void
37
    {
38
        $schema = $capsule->getSchema($this->getTable());
39
        $database = $this->database ?? '[default]';
0 ignored issues
show
Bug Best Practice introduced by
The property database does not exist on Cycle\Migrations\Operation\Table\PrimaryKeys. Did you maybe forget to declare it?
Loading history...
40
41
        if ($schema->exists()) {
42
            throw new TableException(
43
                "Unable to set primary keys for table '{$database}'.'{$this->getTable()}', table already exists"
44
            );
45
        }
46
47
        $schema->setPrimaryKeys($this->columns);
48
    }
49
}
50