Completed
Branch feature/pre-split (67216b)
by Anton
03:28
created

RenameColumn::execute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 1
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
namespace Spiral\Migrations\Operations\Columns;
9
10
use Spiral\Migrations\CapsuleInterface;
11
use Spiral\Migrations\Exceptions\Operations\ColumnException;
12
use Spiral\Migrations\Operations\TableOperation;
13
14
class RenameColumn extends TableOperation
15
{
16
    /**
17
     * Column name.
18
     *
19
     * @var string
20
     */
21
    private $name = '';
22
23
    /**
24
     * @var string
25
     */
26
    private $newName = '';
27
28
    /**
29
     * @param string $database
30
     * @param string $table
31
     * @param string $name
32
     * @param string $newName
33
     */
34
    public function __construct($database, string $table, string $name, string $newName)
35
    {
36
        parent::__construct($database, $table);
37
38
        $this->name = $name;
39
        $this->newName = $newName;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function execute(CapsuleInterface $capsule)
46
    {
47
        $schema = $capsule->getSchema($this->getTable(), $this->getDatabase());
48
49
        if (!$schema->hasColumn($this->name)) {
50
            throw new ColumnException(
51
                "Unable to drop column '{$schema->getName()}'.'{$this->name}', column does not exists"
52
            );
53
        }
54
55
        if ($schema->hasColumn($this->newName)) {
56
            throw new ColumnException(
57
                "Unable to rename column '{$schema->getName()}'.'{$this->name}', column '{$this->newName}' already exists"
58
            );
59
        }
60
61
        //Declaring column
62
        $schema->renameColumn($this->name, $this->newName);
63
    }
64
}