Completed
Pull Request — master (#66)
by
unknown
01:43
created

BaseCommand   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 6

Importance

Changes 0
Metric Value
wmc 13
lcom 3
cbo 6
dl 0
loc 57
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getTemplate() 0 4 1
A getArgumentParser() 0 4 1
A save() 0 10 5
A makeDirectory() 0 6 2
A spaces() 0 4 1
A getNamespace() 0 8 2
1
<?php namespace Wn\Generators\Commands;
2
3
use Illuminate\Console\Command;
4
use Illuminate\Filesystem\Filesystem;
5
use Wn\Generators\Argument\ArgumentFormatLoader;
6
use Wn\Generators\Argument\ArgumentParser;
7
use Wn\Generators\Template\TemplateLoader;
8
9
10
class BaseCommand extends Command {
11
12
    protected $fs;
13
	protected $templates;
14
15
	public function __construct(Filesystem $fs)
16
	{
17
        parent::__construct();
18
19
        $this->fs = $fs;
20
        $this->templates = new TemplateLoader($fs);
21
        $this->argumentFormatLoader = new ArgumentFormatLoader($fs);
0 ignored issues
show
Bug introduced by
The property argumentFormatLoader does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
22
    }
23
24
    protected function getTemplate($name)
25
    {
26
        return $this->templates->load($name);
27
    }
28
29
    protected function getArgumentParser($name){
30
        $format = $this->argumentFormatLoader->load($name);
31
        return new ArgumentParser($format);
32
    }
33
34
    protected function save($content, $path, $name, $force = false)
35
    {
36
        if (!$force && $this->fs->exists($path) && $this->input->hasOption('force') && !$this->option('force')) {
37
            $this->info("{$name} already exists; use --force option to override it !");
38
            return;
39
        }
40
        $this->makeDirectory($path);
41
        $this->fs->put($path, $content);
42
        $this->info("{$name} generated in {$path}!");
43
    }
44
45
    protected function makeDirectory($path)
46
    {
47
        if (!$this->fs->isDirectory(dirname($path))) {
48
            $this->fs->makeDirectory(dirname($path), 0777, true, true);
49
        }
50
    }
51
52
    protected function spaces($n)
53
    {
54
        return str_repeat(' ', $n);
55
    }
56
57
    protected function getNamespace($path = false)
58
    {
59
		if (! $path) {
60
			$path = $this->option('path');
61
		}
62
63
    	return str_replace(' ', '\\', ucwords(trim(str_replace('/', ' ', $path))));
64
    }
65
66
}
67