Completed
Push — master ( 06b292...adafac )
by Nicolai
02:22
created

BaseCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
fire() 0 1 ?
provideArguments() 0 1 ?
A getOption() 0 6 2
A getArguments() 0 4 1
A nameIsNotConfigured() 0 4 1
A configureName() 0 9 2
1
<?php
2
3
4
namespace SmartWeb\ModuleTesting\Console\Command;
5
6
use Illuminate\Console\Command;
7
use Illuminate\Support\Str;
8
use SmartWeb\ModuleTesting\Console\CommandArguments;
9
10
11
abstract class BaseCommand extends Command
12
{
13
    
14
    /**
15
     * Namespace for the command.
16
     */
17
    const COMMAND_NAMESPACE = 'smartweb-testing';
18
    
19
    /**
20
     * BaseCommand constructor.
21
     */
22
    public function __construct()
23
    {
24
        $this->configureName();
25
        
26
        parent::__construct();
27
    }
28
    
29
    /**
30
     * Execute the console command.
31
     *
32
     * @return void
33
     */
34
    abstract public function fire();
35
    
36
    /**
37
     * @return CommandArguments
38
     */
39
    abstract protected function provideArguments() : CommandArguments;
40
    
41
    /**
42
     * Get the value of a command option, with an optional fallback if the option is not set.
43
     *
44
     * @param string       $key
45
     * @param string|array $default
46
     *
47
     * @return string|array
48
     */
49
    final protected function getOption(string $key, $default)
50
    {
51
        return $this->hasOption($key)
52
            ? $this->option($key)
53
            : $default;
54
    }
55
    
56
    /**
57
     * Get the console command arguments.
58
     *
59
     * @return array
60
     */
61
    final protected function getArguments()
62
    {
63
        return $this->provideArguments()->toArray();
64
    }
65
    
66
    /**
67
     * @return bool
68
     */
69
    protected function nameIsNotConfigured() : bool
70
    {
71
        return !Str::startsWith($this->name, self::COMMAND_NAMESPACE);
72
    }
73
    
74
    protected function configureName()
75
    {
76
        if ($this->nameIsNotConfigured()) {
77
            static::$defaultName = $this->name;
78
            $this->name = self::COMMAND_NAMESPACE . ':' . $this->name;
79
        } else {
80
            static::$defaultName = Str::replaceFirst(self::COMMAND_NAMESPACE . ':', '', $this->name);
81
        }
82
    }
83
    
84
}