Rename   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 14
dl 0
loc 33
ccs 14
cts 14
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 23 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\Migrations\Operation\Column;
6
7
use Cycle\Migrations\CapsuleInterface;
8
use Cycle\Migrations\Exception\Operation\ColumnException;
9
use Cycle\Migrations\Operation\AbstractOperation;
10
11
final class Rename extends AbstractOperation
12
{
13 32
    public function __construct(string $table, private string $name, private string $newName)
14
    {
15 32
        parent::__construct($table);
16
    }
17
18
    /**
19
     * {@inheritdoc}
20
     */
21 32
    public function execute(CapsuleInterface $capsule): void
22
    {
23 32
        $schema = $capsule->getSchema($this->getTable());
24
25 32
        if (!$schema->hasColumn($this->name)) {
26 8
            throw new ColumnException(
27 8
                "Unable to rename column '{$schema->getName()}'.'{$this->name}', column does not exists"
28
            );
29
        }
30
31 24
        if ($schema->hasColumn($this->newName)) {
32 8
            throw new ColumnException(
33 8
                sprintf(
34
                    "Unable to rename column '%s'.'%s', column '%s' already exists",
35 8
                    $schema->getName(),
36 8
                    $this->name,
37 8
                    $this->newName
38
                )
39
            );
40
        }
41
42
        //Declaring column
43 16
        $schema->renameColumn($this->name, $this->newName);
44
    }
45
}
46