Passed
Branch master (8cb5b2)
by Christopher
04:33 queued 12s
created

Rollback::deleteLogsBySteps()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 4
nop 1
dl 0
loc 16
ccs 10
cts 10
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style introduced by
The PHP open tag does not have a corresponding PHP close tag
Loading history...
2
namespace Triadev\Leopard\Console\Commands\Mapping;
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
4
use Illuminate\Console\ConfirmableTrait;
5
use Triadev\Leopard\Contract\Repository\MappingLogRepositoryContract;
6
7
class Rollback extends BaseCommand
0 ignored issues
show
Coding Style Documentation introduced by
Missing class doc comment
Loading history...
8
{
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration for class Rollback
Loading history...
9
    use ConfirmableTrait;
10
    
11
    /**
12
     * The console command name.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'triadev:mapping:rollback {--steps=}';
0 ignored issues
show
Coding Style introduced by
Protected member variable "signature" must contain a leading underscore
Loading history...
17
    
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Rollback the last migration steps.';
0 ignored issues
show
Coding Style introduced by
Protected member variable "description" must contain a leading underscore
Loading history...
24
    
25
    /** @var MappingLogRepositoryContract */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
26
    private $mappingLogRepository;
0 ignored issues
show
Coding Style introduced by
Private member variable "mappingLogRepository" must contain a leading underscore
Loading history...
Coding Style introduced by
Private member variable "mappingLogRepository" must be prefixed with an underscore
Loading history...
27
    
28
    /**
29
     * Rollback constructor.
30
     * @param MappingLogRepositoryContract $mappingLogRepository
0 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
31
     */
32 132
    public function __construct(MappingLogRepositoryContract $mappingLogRepository)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
33
    {
34 132
        parent::__construct();
35
        
36 132
        $this->mappingLogRepository = $mappingLogRepository;
37 132
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end __construct()
Loading history...
38
    
39
    /**
40
     * Execute the console command.
41
     *
42
     * @throws \Throwable
0 ignored issues
show
introduced by
Comment missing for @throws tag in function comment
Loading history...
43
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
44 4
    public function handle()
45
    {
46 4
        if (!$this->confirmToProceed()) {
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
47
            return;
48
        }
49
        
50 4
        $steps = $this->option('steps') ?: $this->mappingLogRepository->reset();
0 ignored issues
show
Coding Style introduced by
The value of a comparison must not be assigned to a variable
Loading history...
Coding Style introduced by
Inline IF statements are not allowed
Loading history...
51 4
        if (is_int($steps)) {
52 3
            $this->deleteLogsBySteps($steps);
53
        }
54 4
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end handle()
Loading history...
55
    
56
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
57
     * @param int $steps
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected "integer" but found "int" for parameter type
Loading history...
58
     * @throws \Throwable
0 ignored issues
show
introduced by
Comment missing for @throws tag in function comment
Loading history...
59
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
60 3
    private function deleteLogsBySteps(int $steps)
0 ignored issues
show
Coding Style introduced by
Private method name "Rollback::deleteLogsBySteps" must be prefixed with an underscore
Loading history...
61
    {
62 3
        $logs = $this->mappingLogRepository->all();
63
    
64 3
        if ($steps >= count($logs)) {
65 2
            $this->mappingLogRepository->reset();
66
        }
67
    
68 3
        $logs = array_slice(
69 3
            array_reverse($logs),
70 3
            0,
71 3
            $steps
72
        );
73
    
74 3
        foreach ($logs as $log) {
75 3
            $this->mappingLogRepository->delete($log['id']);
76
        }
77 3
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end deleteLogsBySteps()
Loading history...
78
}
0 ignored issues
show
Coding Style introduced by
Expected //end class
Loading history...
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
79