Passed
Push — dbal ( be5115...7a7bb0 )
by Greg
10:42
created

ForeignKey   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 59
rs 10
wmc 5
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2023 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\DB;
21
22
use Doctrine\DBAL\Schema\ForeignKeyConstraint as DBALForeignKey;
23
use Fisharebest\Webtrees\DB;
24
25
use function implode;
26
27
/**
28
 * Simplify constructor arguments for doctrine/dbal.
29
 *
30
 * @internal - use DB::foreignKey()
31
 */
32
final readonly class ForeignKey implements ComponentInterface
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_READONLY, expecting T_CLASS on line 32 at column 6
Loading history...
33
{
34
    /**
35
     * @param array<array-key,string> $local_columns
36
     * @param string                  $foreign_table
37
     * @param array<array-key,string> $foreign_columns
38
     * @param string                  $on_delete
39
     * @param string                  $on_update
40
     */
41
    public function __construct(
42
        private readonly array $local_columns,
43
        private readonly string $foreign_table,
44
        private readonly array $foreign_columns,
45
        private readonly string $on_delete = 'NO ACTION',
46
        private readonly string $on_update = 'NO ACTION',
47
    ) {
48
    }
49
50
    public function onDeleteCascade(): self
51
    {
52
        return new self(
53
            local_columns: $this->local_columns,
54
            foreign_table: $this->foreign_table,
55
            foreign_columns: $this->foreign_columns,
56
            on_delete: 'CASCADE',
57
            on_update: $this->on_update,
58
        );
59
    }
60
61
    public function onDeleteSetNull(): self
62
    {
63
        return new self(
64
            local_columns: $this->local_columns,
65
            foreign_table: $this->foreign_table,
66
            foreign_columns: $this->foreign_columns,
67
            on_delete: 'SET NULL',
68
            on_update: $this->on_update,
69
        );
70
    }
71
72
    public function onUpdateCascade(): self
73
    {
74
        return new self(
75
            local_columns: $this->local_columns,
76
            foreign_table: $this->foreign_table,
77
            foreign_columns: $this->foreign_columns,
78
            on_delete: $this->on_delete,
79
            on_update: 'CASCADE',
80
        );
81
    }
82
83
    public function toDBAL(string $table): DBALForeignKey
84
    {
85
        return new DBALForeignKey(
86
            localColumnNames: $this->local_columns,
87
            foreignTableName: DB::prefix($this->foreign_table),
88
            foreignColumnNames: $this->foreign_columns,
89
            name: DB::prefix(implode('_', ['fk', $table, ...$this->local_columns])),
90
            options: ['onDelete' => $this->on_delete, 'onUpdate' => $this->on_update],
91
        );
92
    }
93
}
94