Completed
Push — master ( c64a17...7959bd )
by Vojta
01:49
created

CommandRunner   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 129
Duplicated Lines 31.78 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 0
dl 41
loc 129
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A run() 13 13 2
A runForPlugin() 15 15 2
A runQueued() 13 13 2
C check() 0 27 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace VojtaSvoboda\WebArtisan\Classes;
2
3
use Artisan;
4
use Config;
5
use VojtaSvoboda\WebArtisan\Models\Settings;
6
7
class CommandRunner
8
{
9
    /** @var array $allowedCommands List of allowed commands loaded from Config. */
10
    protected $allowedCommands;
11
12
    /** @var array $allowedPluginCommands List of allowed plugin commands. */
13
    protected $allowedPluginCommands;
14
15
    /** @var Settings $settings */
16
    protected $settings;
17
18
    /**
19
     * CommandRunner constructor.
20
     *
21
     * @param Settings $settings
22
     */
23
    public function __construct(Settings $settings)
24
    {
25
        $this->settings = $settings;
26
        $this->allowedCommands = Config::get('vojtasvoboda.webartisan::allowedCommands');
27
        $this->allowedPluginCommands = Config::get('vojtasvoboda.webartisan::allowedPluginCommands');
28
    }
29
30
    /**
31
     * Run command and return true if Ok or string with error.
32
     *
33
     * @param string $command
34
     * @param string $hash
35
     *
36
     * @return bool|string
37
     */
38 View Code Duplication
    public function run($command, $hash)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
    {
40
        // check command and hash
41
        $check = $this->check($command, $hash);
42
        if ($check !== true) {
43
            return $check;
44
        }
45
46
        // run command
47
        Artisan::call($command);
48
49
        return true;
50
    }
51
52
    /**
53
     * Run plugin command and return true if Ok or string with error.
54
     *
55
     * @param $command
56
     * @param $plugin
57
     * @param $hash
58
     *
59
     * @return bool|string
60
     */
61 View Code Duplication
    public function runForPlugin($command, $plugin, $hash)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
    {
63
        // check command, plugin and hash
64
        $check = $this->check($command, $hash, $plugin);
65
        if ($check !== true) {
66
            return $check;
67
        }
68
69
        // run command
70
        Artisan::call($command, [
71
            'name' => $plugin,
72
        ]);
73
74
        return true;
75
    }
76
77
    /**
78
     * Run queued command and return true if Ok or string with error.
79
     *
80
     * @param string $command
81
     * @param string $hash
82
     *
83
     * @return bool|string
84
     */
85 View Code Duplication
    public function runQueued($command, $hash)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
    {
87
        // check command and hash
88
        $check = $this->check($command, $hash);
89
        if ($check !== true) {
90
            return $check;
91
        }
92
93
        // run queued command
94
        Artisan::queue($command);
95
96
        return true;
97
    }
98
99
    /**
100
     * Check command and hash.
101
     *
102
     * @param string $command Command identifier.
103
     * @param string $hash Security hash.
104
     * @param string $plugin Plugin identifier (optional).
105
     *
106
     * @return bool|string
107
     */
108
    public function check($command, $hash, $plugin = null)
109
    {
110
        // check commands whitelist
111
        if ($plugin === null && !in_array($command, $this->allowedCommands)) {
112
            return 'This command is not allowed.';
113
        }
114
115
        // check plugin commands whitelist
116
        if ($plugin !== null && !in_array($command, $this->allowedPluginCommands)) {
117
            return 'This command is not allowed.';
118
        }
119
120
        // get hash
121
        $localHash = $this->settings->get('hash');
122
123
        // if hash not set
124
        if (strlen($localHash) < 16) {
125
            return 'You have to set controll hash at backend settings and it should be at least 16 characters length.';
126
        }
127
128
        // if hash doesn't match
129
        if ($hash != $localHash) {
130
            return 'Wrong control hash. Check backend settings.';
131
        }
132
133
        return true;
134
    }
135
}
136