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

AddIndex   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A execute() 0 15 2
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
class AddIndex extends IndexOperation
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->getTable(), $this->getDatabase());
37
38
        if ($schema->hasIndex($this->columns)) {
39
            $columns = join(',', $this->columns);
40
            throw new IndexException(
41
                "Unable to create index '{$schema->getName()}'.({$columns}), index already exists"
42
            );
43
        }
44
45
        $schema->index($this->columns)->unique(
46
            $this->getOption('unique', false)
47
        );
48
    }
49
}