Passed
Pull Request — 2.x (#119)
by Maxim
36:32 queued 16:28
created

AbstractForeignKey::withoutIndex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of Cycle ORM package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Cycle\Database\Schema;
13
14
use Cycle\Database\Driver\DriverInterface;
15
use Cycle\Database\ForeignKeyInterface;
16
use Cycle\Database\Schema\Traits\ElementTrait;
17
18
/**
19
 * Abstract foreign schema with read (see ReferenceInterface) and write abilities. Must be
20
 * implemented by driver to support DBMS specific syntax and creation rules.
21
 */
22
abstract class AbstractForeignKey implements ForeignKeyInterface, ElementInterface
23
{
24
    use ElementTrait;
25
26
    protected const EXCLUDE_FROM_COMPARE = ['createIndex'];
27
28
    /**
29
     * Local column name (key name).
30
     */
31
    protected array $columns = [];
32
33
    /**
34
     * Referenced table name (including prefix).
35
     */
36
    protected string $foreignTable = '';
37
38
    /**
39
     * Linked foreign key name (foreign column).
40
     */
41
    protected array $foreignKeys = [];
42
43
    /**
44
     * Action on foreign column value deletion.
45
     */
46
    protected string $deleteRule = self::NO_ACTION;
47
48
    /**
49
     * Action on foreign column value update.
50
     */
51
    protected string $updateRule = self::NO_ACTION;
52
53
    /**
54
     * Create an index or not.
55
     */
56
    protected bool $createIndex = true;
57 224
58
    /**
59
     * @psalm-param non-empty-string $table
60
     *
61
     * @param string $tablePrefix
62 224
     *
63
     * @psalm-param non-empty-string $name
64 224
     */
65
    public function __construct(
66 224
        protected string $table,
67
        protected string $tablePrefix,
68
        protected string $name
69 134
    ) {
70
    }
71 134
72
    public function getColumns(): array
73
    {
74 184
        return $this->columns;
75
    }
76 184
77
    public function getForeignTable(): string
78
    {
79
        return $this->foreignTable;
80
    }
81
82 184
    public function getForeignKeys(): array
83
    {
84 184
        return $this->foreignKeys;
85
    }
86
87
    /**
88
     * @psalm-return non-empty-string
89
     */
90 184
    public function getDeleteRule(): string
91
    {
92 184
        return $this->deleteRule;
93
    }
94
95
    /**
96
     * @psalm-return non-empty-string
97
     */
98
    public function getUpdateRule(): string
99 224
    {
100
        return $this->updateRule;
101 224
    }
102
103 224
    /**
104
     * Set local column names foreign key relates to. Make sure column type is the same as foreign
105
     * column one.
106
     */
107
    public function columns(array $columns): self
108
    {
109
        $this->columns = $columns;
110
111
        return $this;
112
    }
113
114
    /**
115 224
     * Set foreign table name and key local column must reference to. Make sure local and foreign
116
     * column types are identical.
117
     *
118
     * @@psalm-param non-empty-string $table Foreign table name with or without database prefix (see 3rd argument).
119
     *
120 224
     * @param array $columns Foreign key names (id by default).
121 224
     * @param bool $forcePrefix When true foreign table will get same prefix as table being modified.
122
     */
123 224
    public function references(
124
        string $table,
125
        array $columns = ['id'],
126
        bool $forcePrefix = true
127
    ): self {
128
        $this->foreignTable = ($forcePrefix ? $this->tablePrefix : '') . $table;
129
        $this->foreignKeys = $columns;
130
131 80
        return $this;
132
    }
133 80
134
    /**
135 80
     * Set foreign key delete behaviour.
136
     *
137
     * @psalm-param non-empty-string $rule Possible values: NO ACTION, CASCADE, etc (driver specific).
138
     */
139
    public function onDelete(string $rule = self::NO_ACTION): self
140
    {
141
        $this->deleteRule = strtoupper($rule);
142
143 80
        return $this;
144
    }
145 80
146
    /**
147 80
     * Set foreign key update behaviour.
148
     *
149
     * @psalm-param non-empty-string $rule Possible values: NO ACTION, CASCADE, etc (driver specific).
150
     */
151
    public function onUpdate(string $rule = self::NO_ACTION): self
152
    {
153
        $this->updateRule = strtoupper($rule);
154
155 168
        return $this;
156
    }
157 168
158
    public function withoutIndex(): void
159 168
    {
160 168
        $this->createIndex = false;
161 168
    }
162 168
163
    public function shouldCreateIndex(): bool
164 168
    {
165 168
        return $this->createIndex;
166
    }
167 168
168 168
    /**
169
     * Foreign key creation syntax.
170 168
     *
171
     * @psalm-return non-empty-string
172
     */
173 138
    public function sqlStatement(DriverInterface $driver): string
174
    {
175
        $statement = [];
176 138
177
        $statement[] = 'CONSTRAINT';
178
        $statement[] = $driver->identifier($this->name);
0 ignored issues
show
Bug introduced by
The method identifier() does not exist on Cycle\Database\Driver\DriverInterface. It seems like you code against a sub-type of Cycle\Database\Driver\DriverInterface such as Cycle\Database\Driver\Driver. ( Ignorable by Annotation )

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

178
        /** @scrutinizer ignore-call */ 
179
        $statement[] = $driver->identifier($this->name);
Loading history...
179
        $statement[] = 'FOREIGN KEY';
180
        $statement[] = '(' . $this->packColumns($driver, $this->columns) . ')';
181
182 224
        $statement[] = 'REFERENCES ' . $driver->identifier($this->foreignTable);
183
        $statement[] = '(' . $this->packColumns($driver, $this->foreignKeys) . ')';
184 224
185
        $statement[] = "ON DELETE {$this->deleteRule}";
186
        $statement[] = "ON UPDATE {$this->updateRule}";
187
188
        return implode(' ', $statement);
189
    }
190
191
    public function compare(self $initial): bool
192
    {
193
        // soft compare
194
        if ($this == clone $initial) {
195
            return true;
196
        }
197
        $initialVars = \get_object_vars($initial);
198
199
        foreach (\get_object_vars($this) as $name => $value) {
200
            if (\in_array($name, static::EXCLUDE_FROM_COMPARE, true)) {
201
                continue;
202
            }
203
204
            if ($value !== $initialVars[$name]) {
205
                return false;
206
            }
207
        }
208
209
        return true;
210
    }
211
212
    /**
213
     * @psalm-return non-empty-string
214
     */
215
    protected function packColumns(DriverInterface $driver, array $columns): string
216
    {
217
        return implode(', ', array_map([$driver, 'identifier'], $columns));
218
    }
219
}
220