Rename::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
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