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

DropColumn::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 6
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 DropColumn extends TableOperation
15
{
16
    /**
17
     * Column name.
18
     *
19
     * @var string
20
     */
21
    private $name = '';
22
23
    /**
24
     * @param string|null $database
25
     * @param string      $table
26
     * @param string      $name
27
     */
28
    public function __construct($database, string $table, string $name)
29
    {
30
        parent::__construct($database, $table);
31
32
        $this->name = $name;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function execute(CapsuleInterface $capsule)
39
    {
40
        $schema = $capsule->getSchema($this->getTable(), $this->getDatabase());
41
42
        if (!$schema->hasColumn($this->name)) {
43
            throw new ColumnException(
44
                "Unable to drop column '{$schema->getName()}'.'{$this->name}', column does not exists"
45
            );
46
        }
47
48
        //Declaring column
49
        $schema->dropColumn($this->name);
50
    }
51
}