Rename   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 12
dl 0
loc 29
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A execute() 0 19 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