Migrations   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 53
ccs 0
cts 23
cp 0
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 12 4
A createMigration() 0 10 1
1
<?php
2
3
namespace Distilleries\Contentful\Commands\Generators;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Support\Carbon;
7
8
class Migrations extends AbstractGenerator
9
{
10
    /**
11
     * {@inheritdoc}
12
     */
13
    protected $signature = 'contentful:generate-migrations';
14
15
    /**
16
     * {@inheritdoc}
17
     */
18
    protected $description = 'Generate migrations from Contentful content-types';
19
20
    /**
21
     * Execute the console command.
22
     *
23
     * @return void
24
     * @throws \GuzzleHttp\Exception\GuzzleException
25
     * @throws \Exception
26
     */
27
    public function handle()
28
    {
29
        $contentTypes = $this->api->contentTypes();
30
31
        if (! empty($contentTypes['items'])) {
32
            array_unshift($contentTypes['items'], $this->assetContentType());
33
            foreach ($contentTypes['items'] as $contentType) {
34
                if ($contentType['sys']['id'] !== 'asset') {
35
                    $this->info('Content-Type: ' . Str::upper($contentType['name']));
36
                    $file = $this->createMigration($contentType);
37
                    $this->line('Migration "' . $file . '" created');
38
                    sleep(1);
39
                }
40
            }
41
        }
42
    }
43
44
    /**
45
     * Create migration file for given content-type.
46
     *
47
     * @param  array  $contentType
48
     * @return string
49
     * @throws \Exception
50
     */
51
    private function createMigration($contentType): string
52
    {
53
        $table = $this->tableName($contentType['sys']['id']);
54
55
        $stubPath = __DIR__ . '/stubs/migration.stub';
56
        $destPath = database_path('migrations/' . Carbon::now()->format('Y_m_d_His') . '_create_' . $table . '_table.php');
57
58
        return static::writeStub($stubPath, $destPath, [
59
            'class' => Str::studly($table),
60
            'table' => $table
61
        ]);
62
    }
63
}
64