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

Rename   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
eloc 18
c 1
b 0
f 1
dl 0
loc 46
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A execute() 0 23 3
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\Column;
13
14
use Cycle\Migrations\CapsuleInterface;
15
use Cycle\Migrations\Exception\Operation\ColumnException;
16
use Cycle\Migrations\Operation\AbstractOperation;
17
18
final class Rename extends AbstractOperation
19
{
20
    /** @var string */
21
    private $name = '';
22
23
    /** @var string */
24
    private $newName = '';
25
26
    /**
27
     * @param string $table
28
     * @param string $name
29
     * @param string $newName
30
     */
31
    public function __construct(string $table, string $name, string $newName)
32
    {
33
        parent::__construct($table);
34
        $this->name = $name;
35
        $this->newName = $newName;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function execute(CapsuleInterface $capsule): void
42
    {
43
        $schema = $capsule->getSchema($this->getTable());
44
45
        if (!$schema->hasColumn($this->name)) {
46
            throw new ColumnException(
47
                "Unable to rename column '{$schema->getName()}'.'{$this->name}', column does not exists"
48
            );
49
        }
50
51
        if ($schema->hasColumn($this->newName)) {
52
            throw new ColumnException(
53
                sprintf(
54
                    "Unable to rename column '%s'.'%s', column '%s' already exists",
55
                    $schema->getName(),
56
                    $this->name,
57
                    $this->newName
58
                )
59
            );
60
        }
61
62
        //Declaring column
63
        $schema->renameColumn($this->name, $this->newName);
64
    }
65
}
66