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

Create::execute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 18
c 0
b 0
f 0
rs 9.9666
cc 3
nc 3
nop 1
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