Passed
Push — dbal ( 92f26f...aec129 )
by Greg
06:59
created

ForeignKey::onUpdateCascade()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2024 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
/**
26
 * Fluent/immutable constructors for doctrine/dbal.
27
 */
28
final class ForeignKey extends DBALForeignKey
29
{
30
    /**
31
     * @param array<int,string> $local_columns
32
     * @param array<int,string> $foreign_columns
33
     */
34
    public function __construct(
35
        private readonly array $local_columns,
36
        private readonly string $foreign_table,
37
        private readonly array $foreign_columns,
38
        private readonly string $on_delete = 'NO ACTION',
39
        private readonly string $on_update = 'NO ACTION',
40
        string $name = '',
41
    ) {
42
        parent::__construct(
43
            localColumnNames: $this->local_columns,
44
            foreignTableName: DB::prefix($this->foreign_table),
45
            foreignColumnNames: $this->foreign_columns,
46
            name: $name,
47
            options: ['onDelete' => $this->on_delete, 'onUpdate' => $this->on_update],
48
        );
49
    }
50
51
    public function onDeleteCascade(): self
52
    {
53
        return new self(
54
            local_columns: $this->local_columns,
55
            foreign_table: $this->foreign_table,
56
            foreign_columns: $this->foreign_columns,
57
            on_delete: 'CASCADE',
58
            on_update: $this->on_update,
59
        );
60
    }
61
62
    public function onDeleteSetNull(): self
63
    {
64
        return new self(
65
            local_columns: $this->local_columns,
66
            foreign_table: $this->foreign_table,
67
            foreign_columns: $this->foreign_columns,
68
            on_delete: 'SET NULL',
69
            on_update: $this->on_update,
70
        );
71
    }
72
73
    public function onUpdateCascade(): self
74
    {
75
        return new self(
76
            local_columns: $this->local_columns,
77
            foreign_table: $this->foreign_table,
78
            foreign_columns: $this->foreign_columns,
79
            on_delete: $this->on_delete,
80
            on_update: 'CASCADE',
81
        );
82
    }
83
84
    public function name(string $name): self
85
    {
86
        return new self(
87
            local_columns: $this->local_columns,
88
            foreign_table: $this->foreign_table,
89
            foreign_columns: $this->foreign_columns,
90
            on_delete: $this->on_delete,
91
            on_update: $this->on_update,
92
            name: $name,
93
        );
94
    }
95
}
96