Test Setup Failed
Pull Request — master (#375)
by
unknown
63:41
created

MigrationCreator::getTranslationSuffix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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