BaseDefinition   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 83
ccs 0
cts 24
cp 0
rs 10
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A snakeId() 0 3 1
A studlyId() 0 3 1
A getStub() 0 9 2
A __construct() 0 5 1
A id() 0 3 1
1
<?php
2
3
namespace Distilleries\Contentful\Commands\Generators\Definitions;
4
5
use Illuminate\Support\Str;
6
7
abstract class BaseDefinition implements DefinitionInterface
8
{
9
    /**
10
     * Model table.
11
     *
12
     * @var string
13
     */
14
    protected $table;
15
16
    /**
17
     * Field data.
18
     *
19
     * @var array
20
     */
21
    protected $field;
22
23
    /**
24
     * BaseDefinition constructor.
25
     *
26
     * @param  string  $table
27
     * @param  array  $field
28
     * @return void
29
     */
30
    public function __construct($table, $field)
31
    {
32
        $this->table = $table;
33
34
        $this->field = $field;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    abstract public function modelGetter();
41
42
    /**
43
     * Return normalized ID of current field.
44
     *
45
     * @return string
46
     */
47
    protected function id(): string
48
    {
49
        return $this->field['id'];
50
    }
51
52
    /**
53
     * Return studly case ID of current field.
54
     *
55
     * @return string
56
     */
57
    protected function studlyId(): string
58
    {
59
        return Str::studly($this->id());
60
    }
61
62
    /**
63
     * Return snake case ID of current field.
64
     *
65
     * @return string
66
     */
67
    protected function snakeId(): string
68
    {
69
        return Str::snake($this->id());
70
    }
71
72
    /**
73
     * Write stub to destination path with given string replacements.
74
     *
75
     * Return relative base path of destination path.
76
     *
77
     * @param  string  $stubPath
78
     * @param  array  $replacements
79
     * @return string
80
     */
81
    public static function getStub(string $stubPath, array $replacements = []): string
82
    {
83
        $content = file_get_contents($stubPath);
84
85
        foreach ($replacements as $key => $value) {
86
            $content = str_replace('{{' . Str::upper($key) . '}}', $value, $content);
87
        }
88
89
        return $content;
90
    }
91
}
92