Add::execute()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 17
ccs 10
cts 10
cp 1
rs 9.9666
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\Migrations\Operation\Index;
6
7
use Cycle\Migrations\CapsuleInterface;
8
use Cycle\Migrations\Exception\Operation\IndexException;
9
use Cycle\Migrations\Operation\Traits\OptionsTrait;
10
11
final class Add extends Index
12
{
13
    use OptionsTrait;
0 ignored issues
show
Bug introduced by
The trait Cycle\Migrations\Operation\Traits\OptionsTrait requires the property $aliases which is not provided by Cycle\Migrations\Operation\Index\Add.
Loading history...
14
15 192
    public function __construct(string $table, array $columns, array $options = [])
16
    {
17 192
        $this->options = $options;
18 192
        parent::__construct($table, $columns);
19
    }
20
21
    /**
22
     * {@inheritdoc}
23
     */
24 184
    public function execute(CapsuleInterface $capsule): void
25
    {
26 184
        $schema = $capsule->getSchema($this->getTable());
27
28 184
        if ($schema->hasIndex($this->columns)) {
29 8
            $columns = implode(',', $this->columns);
30 8
            throw new IndexException(
31 8
                "Unable to create index '{$schema->getName()}'.({$columns}), index already exists"
32
            );
33
        }
34
35 184
        $schema->index($this->columns)->unique(
36 184
            $this->getOption('unique', false)
37
        );
38
39 184
        if ($this->hasOption('name')) {
40 112
            $schema->index($this->columns)->setName($this->getOption('name'));
41
        }
42
    }
43
}
44