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

Rename::__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\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 Rename extends AbstractOperation
20
{
21
    /** @var string */
22
    private $newName = '';
23
24
    /**
25
     * @param string $table
26
     * @param string $newName
27
     */
28
    public function __construct(string $table, string $newName)
29
    {
30
        parent::__construct($table);
31
        $this->newName = $newName;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function execute(CapsuleInterface $capsule): void
38
    {
39
        $schema = $capsule->getSchema($this->getTable());
40
        $database = $this->database ?? '[default]';
0 ignored issues
show
Bug Best Practice introduced by
The property database does not exist on Cycle\Migrations\Operation\Table\Rename. Did you maybe forget to declare it?
Loading history...
41
42
        if (!$schema->exists()) {
43
            throw new TableException(
44
                "Unable to rename table '{$database}'.'{$this->getTable()}', table does not exists"
45
            );
46
        }
47
48
        if ($capsule->getDatabase()->hasTable($this->newName)) {
49
            throw new TableException(
50
                "Unable to rename table '{$database}'.'{$this->getTable()}', table '{$this->newName}' already exists"
51
            );
52
        }
53
54
        $schema->setName($this->newName);
55
        $schema->save(HandlerInterface::DO_ALL);
56
    }
57
}
58