Passed
Push — 2.x ( b9b47b...d5b1be )
by Aleksei
20:07
created

AbstractForeignKey::hasIndex()   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 = ['index'];
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 $index = 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 setIndex(bool $index = true): static
159 168
    {
160 168
        $this->index = $index;
161 168
162 168
        return $this;
163
    }
164 168
165 168
    public function hasIndex(): bool
166
    {
167 168
        return $this->index;
168 168
    }
169
170 168
    /**
171
     * Foreign key creation syntax.
172
     *
173 138
     * @psalm-return non-empty-string
174
     */
175
    public function sqlStatement(DriverInterface $driver): string
176 138
    {
177
        $statement = [];
178
179
        $statement[] = 'CONSTRAINT';
180
        $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

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