Completed
Branch feature/pre-split (67216b)
by Anton
03:22
created

PrimaryKeys   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A execute() 0 13 2
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
namespace Spiral\Migrations\Operations\Table;
9
10
use Spiral\Migrations\CapsuleInterface;
11
use Spiral\Migrations\Exceptions\Operations\TableException;
12
use Spiral\Migrations\Operations\TableOperation;
13
14
class PrimaryKeys extends TableOperation
15
{
16
    /**
17
     * @var array
18
     */
19
    private $columns = [];
20
21
    /**
22
     * @param string|null $database
23
     * @param string      $table
24
     * @param array       $columns
25
     */
26
    public function __construct($database, string $table, array $columns)
27
    {
28
        parent::__construct($database, $table);
29
        $this->columns = $columns;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function execute(CapsuleInterface $capsule)
36
    {
37
        $schema = $capsule->getSchema($this->getTable(), $this->getDatabase());
38
        $database = $this->database ?? '[default]';
39
40
        if ($schema->exists()) {
41
            throw new TableException(
42
                "Unable to set primary keys for table '{$database}'.'{$this->getTable()}', table already exists"
43
            );
44
        }
45
46
        $schema->setPrimaryKeys($this->columns);
47
    }
48
}