Test Setup Failed
Pull Request — master (#374)
by
unknown
62:27
created

TranslateTableCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 6
dl 0
loc 127
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A fire() 0 18 1
A getTableName() 0 5 1
A getMigrationName() 0 4 1
A writeMigration() 0 8 1
A getMigrationPath() 0 8 2
1
<?php
2
3
namespace Dimsav\Translatable\Console;
4
5
use Illuminate\Database\Console\Migrations\BaseCommand;
6
use Illuminate\Support\Composer;
7
use Illuminate\Support\Facades\App;
8
use Illuminate\Support\Str;
9
10
class TranslateTableCommand extends BaseCommand
11
{
12
    /**
13
     * The console command signature.
14
     *
15
     * @var string
16
     */
17
    protected $signature = 'translate:table {name : The name of the translate table.}
18
        {--path= : The location where the migration file should be created.}';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Create a new translate migration file';
26
27
    /**
28
     * The migration creator instance.
29
     *
30
     * @var \Illuminate\Database\Migrations\MigrationCreator
31
     */
32
    protected $creator;
33
34
    /**
35
     * The Composer instance.
36
     *
37
     * @var \Illuminate\Support\Composer
38
     */
39
    protected $composer;
40
41
    /**
42
     * Create a new migration install command instance.
43
     *
44
     * @param  \Dimsav\Translatable\Console\MigrationCreator  $creator
45
     * @param  \Illuminate\Support\Composer  $composer
46
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
47
     */
48
    public function __construct(MigrationCreator $creator, Composer $composer)
49
    {
50
        parent::__construct();
51
52
        $this->creator = $creator;
53
        $this->composer = $composer;
54
    }
55
56
57
    /**
58
     * Execute the console command.
59
     *
60
     * @return void
61
     */
62
    public function fire()
63
    {
64
        // It's possible for the developer to specify the tables to modify in this
65
        // schema operation. The developer may also specify if this table needs
66
        // to be freshly created so we can create the appropriate migrations.
67
        $name = $this->getMigrationName(trim($this->input->getArgument('name')));
68
69
        $table = $this->getTableName(trim($this->input->getArgument('name')));
70
        $create = true;
71
72
73
        // Now we are ready to write the migration out to disk. Once we've written
74
        // the migration out, we will dump-autoload for the entire framework to
75
        // make sure that the migrations are registered by the class loaders.
76
        $this->writeMigration($name, $table, $create);
77
78
        $this->composer->dumpAutoloads();
79
    }
80
81
    /**
82
     * Get the translate table name.
83
     *
84
     * @param string $table
85
     * @return string
86
     */
87
    protected function getTableName($table)
88
    {
89
        $translation_suffix = Str::snake(Str::plural(App::make('config')->get('translatable.translation_suffix', 'translation')));
90
        return Str::snake(Str::singular(class_basename($table)).'_'.$translation_suffix);
91
    }
92
93
    /**
94
     * Get the translate migration file name.
95
     *
96
     * @param string $name
97
     * @return string
98
     */
99
    protected function getMigrationName($name)
100
    {
101
        return 'create_'. $this->getTableName($name) .'_table';
102
    }
103
104
105
106
    /**
107
     * Write the migration file to disk.
108
     *
109
     * @param  string  $name
110
     * @param  string  $table
111
     * @param  bool    $create
112
     * @return string
113
     */
114
    protected function writeMigration($name, $table, $create)
115
    {
116
        $file = pathinfo($this->creator->create(
117
            $name, $this->getMigrationPath(), $table, $create
118
        ), PATHINFO_FILENAME);
119
120
        $this->line("<info>Created Migration:</info> {$file}");
121
    }
122
123
    /**
124
     * Get migration path (either specified by '--path' option or default location).
125
     *
126
     * @return string
127
     */
128
    protected function getMigrationPath()
129
    {
130
        if (! is_null($targetPath = $this->input->getOption('path'))) {
131
            return $this->laravel->basePath().'/'.$targetPath;
132
        }
133
134
        return parent::getMigrationPath();
135
    }
136
}
137