PartialDown   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 80
Duplicated Lines 32.5 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 26 26 3
A getDownFilePayload() 0 9 1
A getRetryTime() 0 6 3

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
2
3
namespace DigiFactory\PartialDown\Commands;
4
5
use Exception;
6
use Illuminate\Console\Command;
7
use Illuminate\Support\InteractsWithTime;
8
use Illuminate\Support\Str;
9
10
class PartialDown extends Command
11
{
12
    use InteractsWithTime;
13
14
    /**
15
     * The console command signature.
16
     *
17
     * @var string
18
     */
19
    protected $signature = 'partial-down {part}
20
                                         {--message= : The message for the maintenance mode}
21
                                         {--retry= : The number of seconds after which the request may be retried}
22
                                         {--allow=* : IP or networks allowed to access the application while in maintenance mode}';
23
24
    /**
25
     * The console command description.
26
     *
27
     * @var string
28
     */
29
    protected $description = 'Put specific part of the application into maintenance mode';
30
31
    /**
32
     * Execute the console command.
33
     *
34
     * @return int
35
     */
36 View Code Duplication
    public function handle()
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...
37
    {
38
        $part = Str::slug($this->argument('part'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('part') targeting Illuminate\Console\Conce...ractsWithIO::argument() can also be of type array or null; however, Illuminate\Support\Str::slug() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
39
40
        $lockFilename = 'framework/partial-down-'.$part;
41
42
        try {
43
            if (file_exists(storage_path($lockFilename))) {
44
                $this->comment("This part [{$part}] of the application is already down.");
45
46
                return true;
47
            }
48
49
            file_put_contents(storage_path($lockFilename),
50
                json_encode($this->getDownFilePayload(),
51
                    JSON_PRETTY_PRINT));
52
53
            $this->comment("This part [{$part}] of the application is now in maintenance mode.");
54
        } catch (Exception $e) {
55
            $this->error("Failed to put this part [{$part}] of the application in maintenance mode.");
56
57
            $this->error($e->getMessage());
58
59
            return 1;
60
        }
61
    }
62
63
    /**
64
     * Get the payload to be placed in the "down" file.
65
     *
66
     * @return array
67
     */
68
    protected function getDownFilePayload()
69
    {
70
        return [
71
            'time' => $this->currentTime(),
72
            'message' => $this->option('message'),
73
            'retry' => $this->getRetryTime(),
74
            'allowed' => $this->option('allow'),
75
        ];
76
    }
77
78
    /**
79
     * Get the number of seconds the client should wait before retrying their request.
80
     *
81
     * @return int|null
82
     */
83
    protected function getRetryTime()
84
    {
85
        $retry = $this->option('retry');
86
87
        return is_numeric($retry) && $retry > 0 ? (int) $retry : null;
88
    }
89
}
90