Rename::execute()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 19
ccs 11
cts 11
cp 1
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\Migrations\Operation\Table;
6
7
use Cycle\Database\Driver\HandlerInterface;
8
use Cycle\Migrations\CapsuleInterface;
9
use Cycle\Migrations\Exception\Operation\TableException;
10
use Cycle\Migrations\Operation\AbstractOperation;
11
12
final class Rename extends AbstractOperation
13
{
14 24
    public function __construct(string $table, private string $newName)
15
    {
16 24
        parent::__construct($table);
17
    }
18
19
    /**
20
     * {@inheritdoc}
21
     */
22 24
    public function execute(CapsuleInterface $capsule): void
23
    {
24 24
        $schema = $capsule->getSchema($this->getTable());
25 24
        $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...
26
27 24
        if (!$schema->exists()) {
28 8
            throw new TableException(
29 8
                "Unable to rename table '{$database}'.'{$this->getTable()}', table does not exists"
30
            );
31
        }
32
33 16
        if ($capsule->getDatabase()->hasTable($this->newName)) {
34 8
            throw new TableException(
35 8
                "Unable to rename table '{$database}'.'{$this->getTable()}', table '{$this->newName}' already exists"
36
            );
37
        }
38
39 8
        $schema->setName($this->newName);
40 8
        $schema->save(HandlerInterface::DO_ALL);
41
    }
42
}
43