AddForeignColumnStub   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 27
c 1
b 0
f 0
dl 0
loc 71
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A getPopulatedContent() 0 11 1
A getStubsPath() 0 3 1
A getStub() 0 7 2
A getFilename() 0 3 1
A getClass() 0 3 1
1
<?php
2
3
/*
4
 * This file is part of Laravel Love.
5
 *
6
 * (c) Anton Komarev <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Cog\Laravel\Love\Support\Database;
15
16
use Illuminate\Filesystem\Filesystem;
17
use Illuminate\Support\Str;
18
19
final class AddForeignColumnStub
20
{
21
    private Filesystem $files;
22
23
    private string $table;
24
25
    private string $referencedTable;
26
27
    private string $foreignColumn;
28
29
    private bool $isForeignColumnNullable;
30
31
    private string $referencedColumn;
32
33
    public function __construct(
34
        Filesystem $files,
35
        string $table,
36
        string $referencedTable,
37
        string $foreignColumn,
38
        string $referencedColumn,
39
        bool $isForeignColumnNullable,
40
    ) {
41
        $this->files = $files;
42
        $this->table = $table;
43
        $this->referencedTable = $referencedTable;
44
        $this->foreignColumn = $foreignColumn;
45
        $this->referencedColumn = $referencedColumn;
46
        $this->isForeignColumnNullable = $isForeignColumnNullable;
47
    }
48
49
    public function getFilename(): string
50
    {
51
        return sprintf('add_%s_to_%s_table', $this->foreignColumn, $this->table);
52
    }
53
54
    public function getClass(): string
55
    {
56
        return Str::studly($this->getFilename());
57
    }
58
59
    private function getStubsPath(): string
60
    {
61
        return __DIR__ . '/Stubs';
62
    }
63
64
    /**
65
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
66
     */
67
    private function getStub(): string
68
    {
69
        $filename = $this->isForeignColumnNullable
70
            ? 'AddForeignNullableColumn.stub'
71
            : 'AddForeignColumn.stub';
72
73
        return $this->files->get("{$this->getStubsPath()}/{$filename}");
74
    }
75
76
    /**
77
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
78
     */
79
    public function getPopulatedContent(): string
80
    {
81
        $stub = $this->getStub();
82
83
        $stub = str_replace('DummyClass', $this->getClass(), $stub);
84
        $stub = str_replace('DummyTable', $this->table, $stub);
85
        $stub = str_replace('DummyForeignColumn', $this->foreignColumn, $stub);
86
        $stub = str_replace('DummyReferencedTable', $this->referencedTable, $stub);
87
        $stub = str_replace('DummyReferencedColumn', $this->referencedColumn, $stub);
88
89
        return $stub;
90
    }
91
}
92