Completed
Push — master ( 3394d6...2a04c8 )
by Zach
03:20
created

CreateCleaner::writeCleaner()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 2
1
<?php
2
3
namespace LaravelLaundromat\commands;
4
5
use File;
6
use Illuminate\Console\Command;
7
use Illuminate\Console\AppNamespaceDetectorTrait;
8
9
class CreateCleaner extends Command
10
{
11
    use AppNamespaceDetectorTrait;
12
13
    /**
14
     * The name and signature of the console command.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'laundromat:create {name}';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Create a new cleaner class.';
26
27
    /**
28
     * Execute the console command.
29
     *
30
     * @return mixed
31
     */
32
    public function handle()
33
    {
34
        $this->createDirectory();
35
36
        $namespace = $this->getAppNamespace().'Cleaners';
37
38
        $name = $this->argument('name');
39
40
        $cleanerName = $namespace.'\\'.$name;
41
42
        if (class_exists($cleanerName)) {
43
            $this->error("Cleaner {$name} already exists!");
44
45
            return;
46
        }
47
48
        $this->writeCleaner($name, $namespace);
49
50
        $this->info("Cleaner {$name} was successfully created!");
51
    }
52
53
    /**
54
     * Create Cleaners directory if it doesn't exist.
55
     */
56
    protected function createDirectory()
57
    {
58
        if (!File::exists(app_path('Cleaners'))) {
59
            $this->info('Creating Cleaners directory...');
60
61
            File::makeDirectory(app_path('Cleaners'));
62
        }
63
    }
64
65
    /**
66
     * Write the property bag file into the settings folder.
67
     *
68
     * @param string $namespace
69
     */
70
    protected function writeCleaner($name, $namespace)
71
    {
72
        $cleaner = file_get_contents(
73
            __DIR__.'/../Stub.php'
74
        );
75
76
        $cleaner = str_replace('{{namespace}}', $namespace, $cleaner);
77
78
        $cleaner = str_replace('{{className}}', $name, $cleaner);
79
80
        file_put_contents(app_path("Cleaners/{$name}.php"), $cleaner);
81
    }
82
}
83