Passed
Pull Request — 2.x (#119)
by Maxim
20:20
created

AbstractForeignKey::packColumns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
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
    /**
27
     * Local column name (key name).
28
     */
29
    protected array $columns = [];
30
31
    /**
32
     * Referenced table name (including prefix).
33
     */
34
    protected string $foreignTable = '';
35
36
    /**
37
     * Linked foreign key name (foreign column).
38
     */
39
    protected array $foreignKeys = [];
40
41
    /**
42
     * Action on foreign column value deletion.
43
     */
44
    protected string $deleteRule = self::NO_ACTION;
45
46
    /**
47
     * Action on foreign column value update.
48
     */
49
    protected string $updateRule = self::NO_ACTION;
50
51
    /**
52
     * Create an index or not.
53
     */
54
    protected bool $createIndex = true;
55
56
    /**
57 224
     * @psalm-param non-empty-string $table
58
     *
59
     * @param string $tablePrefix
60
     *
61
     * @psalm-param non-empty-string $name
62 224
     */
63
    public function __construct(
64 224
        protected string $table,
65
        protected string $tablePrefix,
66 224
        protected string $name
67
    ) {
68
    }
69 134
70
    public function getColumns(): array
71 134
    {
72
        return $this->columns;
73
    }
74 184
75
    public function getForeignTable(): string
76 184
    {
77
        return $this->foreignTable;
78
    }
79
80
    public function getForeignKeys(): array
81
    {
82 184
        return $this->foreignKeys;
83
    }
84 184
85
    /**
86
     * @psalm-return non-empty-string
87
     */
88
    public function getDeleteRule(): string
89
    {
90 184
        return $this->deleteRule;
91
    }
92 184
93
    /**
94
     * @psalm-return non-empty-string
95
     */
96
    public function getUpdateRule(): string
97
    {
98
        return $this->updateRule;
99 224
    }
100
101 224
    /**
102
     * Set local column names foreign key relates to. Make sure column type is the same as foreign
103 224
     * column one.
104
     */
105
    public function columns(array $columns): self
106
    {
107
        $this->columns = $columns;
108
109
        return $this;
110
    }
111
112
    /**
113
     * Set foreign table name and key local column must reference to. Make sure local and foreign
114
     * column types are identical.
115 224
     *
116
     * @@psalm-param non-empty-string $table Foreign table name with or without database prefix (see 3rd argument).
117
     *
118
     * @param array $columns Foreign key names (id by default).
119
     * @param bool $forcePrefix When true foreign table will get same prefix as table being modified.
120 224
     */
121 224
    public function references(
122
        string $table,
123 224
        array $columns = ['id'],
124
        bool $forcePrefix = true
125
    ): self {
126
        $this->foreignTable = ($forcePrefix ? $this->tablePrefix : '') . $table;
127
        $this->foreignKeys = $columns;
128
129
        return $this;
130
    }
131 80
132
    /**
133 80
     * Set foreign key delete behaviour.
134
     *
135 80
     * @psalm-param non-empty-string $rule Possible values: NO ACTION, CASCADE, etc (driver specific).
136
     */
137
    public function onDelete(string $rule = self::NO_ACTION): self
138
    {
139
        $this->deleteRule = strtoupper($rule);
140
141
        return $this;
142
    }
143 80
144
    /**
145 80
     * Set foreign key update behaviour.
146
     *
147 80
     * @psalm-param non-empty-string $rule Possible values: NO ACTION, CASCADE, etc (driver specific).
148
     */
149
    public function onUpdate(string $rule = self::NO_ACTION): self
150
    {
151
        $this->updateRule = strtoupper($rule);
152
153
        return $this;
154
    }
155 168
156
    public function withoutIndex(): void
157 168
    {
158
        $this->createIndex = false;
159 168
    }
160 168
161 168
    public function shouldCreateIndex(): bool
162 168
    {
163
        return $this->createIndex;
164 168
    }
165 168
166
    /**
167 168
     * Foreign key creation syntax.
168 168
     *
169
     * @psalm-return non-empty-string
170 168
     */
171
    public function sqlStatement(DriverInterface $driver): string
172
    {
173 138
        $statement = [];
174
175
        $statement[] = 'CONSTRAINT';
176 138
        $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

176
        /** @scrutinizer ignore-call */ 
177
        $statement[] = $driver->identifier($this->name);
Loading history...
177
        $statement[] = 'FOREIGN KEY';
178
        $statement[] = '(' . $this->packColumns($driver, $this->columns) . ')';
179
180
        $statement[] = 'REFERENCES ' . $driver->identifier($this->foreignTable);
181
        $statement[] = '(' . $this->packColumns($driver, $this->foreignKeys) . ')';
182 224
183
        $statement[] = "ON DELETE {$this->deleteRule}";
184 224
        $statement[] = "ON UPDATE {$this->updateRule}";
185
186
        return implode(' ', $statement);
187
    }
188
189
    public function compare(self $initial): bool
190
    {
191
        // soft compare
192
        return $this == clone $initial;
193
    }
194
195
    /**
196
     * @psalm-return non-empty-string
197
     */
198
    protected function packColumns(DriverInterface $driver, array $columns): string
199
    {
200
        return implode(', ', array_map([$driver, 'identifier'], $columns));
201
    }
202
}
203