Test Setup Failed
Pull Request — master (#375)
by
unknown
115:18 queued 50:25
created

MigrationCreator   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 2
Bugs 2 Features 0
Metric Value
wmc 10
c 2
b 2
f 0
lcom 2
cbo 3
dl 0
loc 105
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getStub() 0 4 1
A populateStub() 0 16 2
A getTranslationSuffix() 0 4 1
A getClassName() 0 4 1
A getPath() 0 4 1
A firePostCreateHooks() 0 6 2
A afterCreate() 0 4 1
A stubPath() 0 4 1
1
<?php
2
3
namespace Dimsav\Translatable\Console;
4
5
use Closure;
6
use Illuminate\Support\Str;
7
use Illuminate\Support\Facades\App;
8
9
class MigrationCreator extends \Illuminate\Database\Migrations\MigrationCreator
10
{
11
    /**
12
     * Get the migration stub file.
13
     *
14
     * @param  string $table
15
     * @param  bool $create
16
     * @return string
17
     */
18
    protected function getStub($table, $create)
19
    {
20
        return $this->files->get($this->stubPath().'/translate.stub');
21
    }
22
23
    /**
24
     * Populate the place-holders in the migration stub.
25
     *
26
     * @param  string $name
27
     * @param  string $stub
28
     * @param  string $table
29
     * @return string
30
     */
31
    protected function populateStub($name, $stub, $table)
32
    {
33
        $stub = str_replace('DummyClass', $this->getClassName($name), $stub);
34
        // Here we will replace the table place-holders with the table specified by
35
        // the developer, which is useful for quickly creating a tables creation
36
        // or update migration from the console instead of typing it manually.
37
        if (! is_null($table)) {
38
            $stub = str_replace('DummyTable', $table, $stub);
39
            $referencesTable = Str::plural(str_replace('_'.$this->getTranslationSuffix(), '', $table));
40
            $stub = str_replace('DummyReferencesTable', $referencesTable, $stub);
41
            $forignKey = Str::singular(str_replace('_'.$this->getTranslationSuffix(), '', $table)).'_id';
42
            $stub = str_replace('DummyForeign', $forignKey, $stub);
43
        }
44
45
        return $stub;
46
    }
47
48
    /**
49
     * Defines the default 'Translation' class suffix.
50
     *
51
     * @return string
52
     */
53
    protected function getTranslationSuffix()
54
    {
55
        return Str::snake(Str::plural(App::make('config')->get('translatable.translation_suffix', 'translation')));
56
    }
57
58
    /**
59
     * Get the class name of a migration name.
60
     *
61
     * @param  string $name
62
     * @return string
63
     */
64
    protected function getClassName($name)
65
    {
66
        return Str::studly($name);
67
    }
68
69
    /**
70
     * Get the full path to the migration.
71
     *
72
     * @param  string $name
73
     * @param  string $path
74
     * @return string
75
     */
76
    protected function getPath($name, $path)
77
    {
78
        return $path.'/'.$this->getDatePrefix().'_'.$name.'.php';
79
    }
80
81
    /**
82
     * Fire the registered post create hooks.
83
     *
84
     * @return void
85
     */
86
    protected function firePostCreateHooks()
87
    {
88
        foreach ($this->postCreate as $callback) {
89
            call_user_func($callback);
90
        }
91
    }
92
93
    /**
94
     * Register a post migration create hook.
95
     *
96
     * @param  \Closure $callback
97
     * @return void
98
     */
99
    public function afterCreate(Closure $callback)
100
    {
101
        $this->postCreate[] = $callback;
102
    }
103
104
    /**
105
     * Get the path to the stubs.
106
     *
107
     * @return string
108
     */
109
    public function stubPath()
110
    {
111
        return __DIR__.'/stubs';
112
    }
113
}