Passed
Push — next ( bea07e...b4e6e6 )
by Bas
05:12 queued 01:45
created

TableCommands::handleKeyCommands()   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7.0222

Importance

Changes 0
Metric Value
cc 7
eloc 12
nc 16
nop 2
dl 0
loc 24
ccs 12
cts 13
cp 0.9231
crap 7.0222
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LaravelFreelancerNL\Aranguent\Schema\Concerns;
6
7
use Illuminate\Support\Fluent;
8
9
trait TableCommands
10
{
11
    /**
12
     * Indicate that the table needs to be created.
13
     *
14
     * @param  array  $options
15
     * @return Fluent
16
     */
17 67
    public function create($options = [])
18
    {
19 67
        $parameters = [];
20 67
        $parameters['options'] = $options;
21 67
        $parameters['explanation'] = "Create '{$this->table}' table.";
22 67
        $parameters['handler'] = 'table';
23
24 67
        return $this->addCommand('create', $parameters);
0 ignored issues
show
Bug introduced by
It seems like addCommand() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
        return $this->/** @scrutinizer ignore-call */ addCommand('create', $parameters);
Loading history...
25
    }
26
27
    /**
28
     * @param string $command
29
     * @param mixed[] $args
30
     * @return void
31
     */
32 49
    public function handleKeyCommands($command, $args)
33
    {
34 49
        $acceptedKeyFields = ['id', '_id', '_key'];
35
36 49
        $columns = ($command === 'autoIncrement') ? end($this->columns) : $args;
37 49
        $columns = (is_array($columns)) ? $columns : [$columns];
38
39 49
        if (count($columns) !== 1 || ! in_array($columns[0], $acceptedKeyFields)) {
40
            return;
41
        }
42
43 49
        if ($command === 'uuid') {
44 2
            $this->keyGenerator = 'uuid';
0 ignored issues
show
Bug Best Practice introduced by
The property keyGenerator does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
45
46 2
            return;
47
        }
48
49 47
        if (config('arangodb.schema.key_handling.use_traditional_over_autoincrement') === false) {
50 2
            $this->keyGenerator = 'autoincrement';
51
52 2
            return;
53
        }
54
55 45
        $this->keyGenerator = 'traditional';
56
    }
57
58 67
    public function executeCreateCommand($command)
59
    {
60 67
        if ($this->connection->pretending()) {
61
            $this->connection->logQuery('/* ' . $command->explanation . " */\n", []);
62
63
            return;
64
        }
65
66 67
        $options = $command->options;
67 67
        $options['keyOptions'] = $this->setKeyOptions($options['keyOptions'] ?? []);
0 ignored issues
show
Bug introduced by
It seems like setKeyOptions() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
        /** @scrutinizer ignore-call */ 
68
        $options['keyOptions'] = $this->setKeyOptions($options['keyOptions'] ?? []);
Loading history...
68
69 67
        if (!$this->schemaManager->hasCollection($this->table)) {
70 66
            $this->schemaManager->createCollection($this->table, $options);
71
        }
72
    }
73
}
74