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

CreateTable   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A execute() 0 19 3
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\Database\Entities\AbstractHandler;
11
use Spiral\Migrations\CapsuleInterface;
12
use Spiral\Migrations\Exceptions\Operations\TableException;
13
use Spiral\Migrations\Operations\TableOperation;
14
15
class CreateTable extends TableOperation
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function execute(CapsuleInterface $capsule)
21
    {
22
        $schema = $capsule->getSchema($this->getTable(), $this->getDatabase());
23
        $database = $this->database ?? '[default]';
24
25
        if ($schema->exists()) {
26
            throw new TableException(
27
                "Unable to create table '{$database}'.'{$this->getTable()}', table already exists"
28
            );
29
        }
30
31
        if (empty($schema->getColumns())) {
32
            throw new TableException(
33
                "Unable to create table '{$database}'.'{$this->getTable()}', no columns were added"
34
            );
35
        }
36
37
        $schema->save(AbstractHandler::DO_ALL);
38
    }
39
}