Completed
Branch feature/pre-split (e801ec)
by Anton
03:11
created

AlterIndex::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 5
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 4
dl 5
loc 5
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\Indexes;
9
10
use Spiral\Migrations\CapsuleInterface;
11
use Spiral\Migrations\Exceptions\Operations\IndexException;
12
use Spiral\Migrations\Operations\IndexOperation;
13
use Spiral\Migrations\Operations\Traits\OptionsTrait;
14
15 View Code Duplication
class AlterIndex extends IndexOperation
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
16
{
17
    use OptionsTrait;
18
19
    /**
20
     * @param string|null $database
21
     * @param string      $table
22
     * @param array       $columns
23
     * @param array       $options
24
     */
25
    public function __construct($database, string $table, array $columns, array $options = [])
26
    {
27
        parent::__construct($database, $table, $columns);
28
        $this->options = $options;
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function execute(CapsuleInterface $capsule)
35
    {
36
        $schema = $capsule->getSchema($this->getDatabase(), $this->getTable());
37
38
        if (!$schema->hasIndex($this->columns)) {
39
            $columns = join(',', $this->columns);
40
            throw new IndexException(
41
                "Unable to alter index '{$schema->getName()}'.({$columns}), index already exists"
42
            );
43
        }
44
45
        $schema->index($this->columns)->unique(
46
            $this->getOption('unique', false)
47
        );
48
    }
49
}