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

Create   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 10
dl 0
loc 23
c 0
b 0
f 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A execute() 0 18 3
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\Database\Driver\HandlerInterface;
15
use Cycle\Migrations\CapsuleInterface;
16
use Cycle\Migrations\Exception\Operation\TableException;
17
use Cycle\Migrations\Operation\AbstractOperation;
18
19
final class Create extends AbstractOperation
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function execute(CapsuleInterface $capsule): void
25
    {
26
        $schema = $capsule->getSchema($this->getTable());
27
        $database = $this->database ?? '[default]';
0 ignored issues
show
Bug Best Practice introduced by
The property database does not exist on Cycle\Migrations\Operation\Table\Create. Did you maybe forget to declare it?
Loading history...
28
29
        if ($schema->exists()) {
30
            throw new TableException(
31
                "Unable to create table '{$database}'.'{$this->getTable()}', table already exists"
32
            );
33
        }
34
35
        if (empty($schema->getColumns())) {
36
            throw new TableException(
37
                "Unable to create table '{$database}'.'{$this->getTable()}', no columns were added"
38
            );
39
        }
40
41
        $schema->save(HandlerInterface::DO_ALL);
42
    }
43
}
44