Passed
Branch next (97e380)
by Bas
03:07
created

Tables::dropIfExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
ccs 0
cts 5
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace LaravelFreelancerNL\Aranguent\Schema\Concerns;
4
5
use Illuminate\Support\Fluent;
6
7
trait Tables
8
{
9
    /**
10
     * Indicate that the table needs to be created.
11
     *
12
     * @param array $options
13
     *
14
     * @return Fluent
15
     */
16 134
    public function create($options = [])
17
    {
18 134
        $parameters = [];
19 134
        $parameters['options'] = $options;
20 134
        $parameters['explanation'] = "Create '{$this->table}' table.";
21 134
        $parameters['handler'] = 'table';
22
23 134
        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

23
        return $this->/** @scrutinizer ignore-call */ addCommand('create', $parameters);
Loading history...
24
    }
25
26
    /**
27
     * Determine if the blueprint has a create command.
28
     *
29
     * @return bool
30
     */
31
    protected function creating()
32
    {
33
        return collect($this->commands)->contains(function ($command) {
34
            return $command->name === 'create';
35
        });
36
    }
37
38 134
    public function executeCreateCommand($command)
39
    {
40 134
        if ($this->connection->pretending()) {
41
            $this->connection->logQuery('/* ' . $command->explanation . " */\n", []);
42
43
            return;
44
        }
45 134
        $options = $command->options;
46 134
        if ($this->temporary === true) {
47
            $options['isVolatile'] = true;
48
        }
49 134
        if ($this->autoIncrement === true) {
50
            $options['keyOptions']['autoincrement'] = true;
51
        }
52
53 134
        if (! $this->schemaManager->hasCollection($this->table)) {
54 3
            $this->schemaManager->createCollection($this->table, $options);
55
        }
56 134
    }
57
58
    /**
59
     * Alias for getCollection.
60
     *
61
     * @return string
62
     */
63
    public function getTable()
64
    {
65
        return $this->table;
66
    }
67
68
    /**
69
     * Rename the table to a given name.
70
     *
71
     * @param string $to
72
     *
73
     * @return Fluent
74
     */
75
    public function rename($to)
76
    {
77
        return $this->addCommand('rename', compact('to'));
78
    }
79
80
    /**
81
     * Indicate that the table should be dropped.
82
     *
83
     * @return Fluent
84
     */
85
    public function drop()
86
    {
87
        $parameters = [];
88
        $parameters['explanation'] = "Drop the '{$this->table}' table.";
89
        $parameters['handler'] = 'table';
90
91
        return $this->addCommand('drop', $parameters);
92
    }
93
94
    /**
95
     * Indicate that the table should be dropped if it exists.
96
     *
97
     * @return Fluent
98
     */
99
    public function dropIfExists()
100
    {
101
        $parameters = [];
102
        $parameters['explanation'] = "Drop the '{$this->table}' table.";
103
        $parameters['handler'] = 'table';
104
105
        return $this->addCommand('dropIfExists');
106
    }
107
}
108