Passed
Push — master ( a8d5c8...21cbf3 )
by Roy
15:41 queued 05:41
created

MakeSynchronizationCommand::getNameWithSuffix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace LaravelSynchronize\Console\Commands;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Console\GeneratorCommand;
7
use LaravelSynchronize\Console\Synchronizer\Synchronizer;
8
9
class MakeSynchronizationCommand extends GeneratorCommand
10
{
11
    /**
12
     * The Synchronizer instance.
13
     *
14
     * @var \LaravelSynchronize\Console\Synchronizer\Synchronizer
15
     */
16
    protected $synchronizer;
17
18
    /**
19
     * The name and signature of the console command.
20
     *
21
     * @var string
22
     */
23
    protected $signature = 'make:synchronization {name}';
24
25
    /**
26
     * The console command description.
27
     *
28
     * @var string
29
     */
30
    protected $description = 'Create a new synchronization file';
31
32
    /**
33
     * The type of class being generated.
34
     *
35
     * @var string
36
     */
37
    protected $type = 'Synchronization';
38
39
    /**
40
     * Create a new controller creator command instance.
41
     *
42
     * @param  \LaravelSynchronize\Console\Synchronizer\Synchronizer  $synchronizer
43
     *
44
     * @return void
45
     */
46
    public function __construct(Synchronizer $synchronizer)
47
    {
48
        $this->synchronizer = $synchronizer;
49
50
        parent::__construct($synchronizer->getFileSystem());
51
    }
52
53
    /**
54
     * Execute the console command.
55
     *
56
     * @return bool|null
57
     */
58
    public function handle()
59
    {
60
        $name = $this->qualifyClass(Str::studly($this->getNameWithSuffix()));
61
        $path = $this->getPath($this->getNameWithSuffix());
62
63
        if ($this->alreadyExists($this->getNameWithSuffix())) {
64
            $this->error($this->type . ' already exists!');
65
66
            return false;
67
        }
68
69
        $this->files->put($path, $this->buildClass($name));
70
        $this->info($this->type . ' created successfully.');
71
    }
72
73
    /**
74
     * Determine if the class already exists.
75
     *
76
     * @param  string  $rawName
77
     *
78
     * @return bool
79
     */
80
    protected function alreadyExists($rawName): bool
81
    {
82
        return $this->synchronizer->hasSynchronization(Str::studly($rawName));
83
    }
84
85
    /**
86
     * Get the destination class name.
87
     *
88
     * @return string
89
     */
90
    protected function getNameWithSuffix(): string
91
    {
92
        return $this->getNameInput() . 'Synchronization';
93
    }
94
95
    /**
96
     * Get the destination class path.
97
     *
98
     * @param  string  $name
99
     *
100
     * @return string
101
     */
102
    protected function getPath($name): string
103
    {
104
        return $this->synchronizer->getDirectory() . '/' . $this->getDatePrefix() . '_' . Str::studly($name) . '.php';
105
    }
106
107
    /**
108
     * Get the stub file for the generator.
109
     *
110
     * @return string
111
     */
112
    protected function getStub(): string
113
    {
114
        return __DIR__ . '/stubs/Synchronization.stub';
115
    }
116
117
    /**
118
     * Get the date prefix for the migration.
119
     *
120
     * @return string
121
     */
122
    protected function getDatePrefix(): string
123
    {
124
        return date('Y_m_d_His');
125
    }
126
}
127