AbstractGenerator::isFieldEnabled()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 6
rs 10
1
<?php
2
3
namespace Distilleries\Contentful\Commands\Generators;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Console\Command;
7
use Illuminate\Support\Facades\DB;
8
use Distilleries\Contentful\Api\ManagementApi as Api;
9
use Distilleries\Contentful\Commands\Generators\Definitions\DefinitionInterface;
10
11
abstract class AbstractGenerator extends Command
12
{
13
    /**
14
     * Contentful Management API implementation.
15
     *
16
     * @var \Distilleries\Contentful\Api\ManagementApi
17
     */
18
    protected $api;
19
20
    /**
21
     * Create a new command instance.
22
     *
23
     * @param  \Distilleries\Contentful\Api\ManagementApi  $api
24
     * @return void
25
     */
26
    public function __construct(Api $api)
27
    {
28
        parent::__construct();
29
30
        $this->api = $api;
31
    }
32
33
    /**
34
     * Return content-type corresponding table string ID.
35
     *
36
     * @param  string  $id
37
     * @return string
38
     */
39
    protected function tableName($id)
40
    {
41
        return DB::getTablePrefix() . Str::plural(Str::snake($id));
42
    }
43
44
    /**
45
     * Write stub to destination path with given string replacements.
46
     *
47
     * Return relative base path of destination path.
48
     *
49
     * @param  string  $stubPath
50
     * @param  string  $destPath
51
     * @param  array  $replacements
52
     * @return string
53
     */
54
    public static function writeStub($stubPath, $destPath, $replacements = []): string
55
    {
56
        $content = file_get_contents($stubPath);
57
        foreach ($replacements as $key => $value) {
58
            $content = str_replace('{{' . Str::upper($key) . '}}', $value, $content);
59
        }
60
61
        file_put_contents($destPath, $content);
62
63
        return str_replace(base_path(), '', $destPath);
64
    }
65
66
    /**
67
     * Asset content-type minimal definition.
68
     *
69
     * @return array
70
     */
71
    protected function assetContentType(): array
72
    {
73
        $assetContentType = [
74
            'sys' => [
75
                'id' => 'asset',
76
            ],
77
            'name' => 'asset',
78
            'fields' => [[
79
                'id' => 'file_name',
80
                'type' => 'Symbol',
81
            ], [
82
                'id' => 'mime_type',
83
                'type' => 'Symbol',
84
            ], [
85
                'id' => 'size',
86
                'type' => 'Integer',
87
            ], [
88
                'id' => 'url',
89
                'type' => 'Symbol',
90
            ], [
91
                'id' => 'title',
92
                'type' => 'Symbol',
93
                'required' => false,
94
            ], [
95
                'id' => 'description',
96
                'type' => 'Symbol',
97
                'required' => false,
98
            ]],
99
        ];
100
101
        $assetContentType['fields'] = array_map(function ($field) {
102
            if (! isset($field['required'])) {
103
                $field['required'] = true;
104
            }
105
            $field['disabled'] = false;
106
            $field['omitted'] = false;
107
108
            return $field;
109
        }, $assetContentType['fields']);
110
111
        return $assetContentType;
112
    }
113
114
    /**
115
     * Return definition interface class instance.
116
     *
117
     * @param  string  $table
118
     * @param  array  $field
119
     * @return \Distilleries\Contentful\Commands\Generators\Definitions\DefinitionInterface
120
     */
121
    protected function fieldDefinition($table, $field): ?DefinitionInterface
122
    {
123
        $className = '\Distilleries\Contentful\Commands\Generators\Definitions\\' . $field['type'] . 'Definition';
124
125
        if (class_exists($className)) {
126
            return new $className($table, $field);
127
        }
128
129
        return null;
130
    }
131
132
    /**
133
     * Return if field is actually enabled.
134
     *
135
     * @param  array  $field
136
     * @return boolean
137
     */
138
    protected function isFieldEnabled($field): bool
139
    {
140
        return ! $field['disabled'] && ! $field['omitted'];
141
    }
142
}
143