Test Setup Failed
Pull Request — master (#3)
by
unknown
05:02
created

ResetQuotaLog::handle()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 63
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 38
CRAP Score 4.3121

Importance

Changes 0
Metric Value
dl 0
loc 63
ccs 38
cts 52
cp 0.7308
rs 8.8945
c 0
b 0
f 0
cc 4
eloc 51
nc 4
nop 0
crap 4.3121

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * This file is part of laravel-quota
5
 *
6
 * (c) David Faith <[email protected]>
7
 *
8
 * Full copyright and license information is available
9
 * in the LICENSE file distributed with this source code.
10
 */
11
12
namespace Projectmentor\Quota\Console\Commands;
13
14
use Illuminate\Console\Command;
15
16
use DB;
17
18
class ResetQuotaLog extends Command
19
{
20
    /**
21
     * The name and signature of the console command.
22
     *
23
     * @var string
24
     */
25
    protected $signature = 'quota:reset {date} {connection} {hits=0} {misses=0}';
26
27
    /**
28
     * The console command description.
29
     *
30
     * @var string
31
     */
32
    protected $description = 'Reset the quotalog counters for a date and connection.';
33
34
    /**
35
     * Create a new command instance.
36
     *
37
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
38
     */
39 8
    public function __construct()
40
    {
41 8
        parent::__construct();
42 8
    }
43
44
    /**
45
     * Execute the console command.
46
     *
47
     * @return mixed
48
     */
49 8
    public function handle()
50
    {
51 8
        switch (DB::connection()->getPDO()->getAttribute(\PDO::ATTR_DRIVER_NAME)) {
52 8
            case 'mysql':
53
                $sql = 'INSERT INTO quotalog' .
54
                ' (date, connection, hits, misses, created_at, updated_at)' .
55
                ' VALUES' .
56
                ' ( :date, :connection, :hits, :misses, :created_at, :updated_at)' .
57
                ' ON DUPLICATE KEY UPDATE' .
58
                ' hits = VALUES(hits),' .
59
                ' misses = VALUES(misses),' .
60
                ' updated_at = VALUES(updated_at)';
61
62
                $this->doUpsertStatement($sql);
63
64
                break;
65 8
            case 'sqlite':
66 8
                $date = $this->argument('date');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
67 8
                $connection = $this->argument('connection');
68
69
                $sql = 'UPDATE quotalog' .
70 8
                ' SET `date` = :date' .
71 8
                ', `connection` = :connection' .
72 8
                ', `hits` = :hits' .
73 8
                ', `misses` = :misses' .
74 8
                ', `updated_at` = :updated_at' .
75 8
                ' WHERE `date` = ' . '\''. $date . '\'' .
76 8
                ' AND `connection` = ' . '\'' . $connection . '\'';
77
78 8
                $this->doUpdateStatement($sql);
79
80 8
                $changes = DB::select('SELECT changes()');
81 8
                if (! $this->sqliteUpdateDidChange($changes)) {
82
                    $sql = 'INSERT INTO quotalog' .
83 8
                      ' (' .
84 8
                      ' `date`' .
85 8
                      ', `connection`' .
86 8
                      ', `hits`' .
87 8
                      ', `misses`' .
88 8
                      ', `created_at`' .
89 8
                      ', `updated_at`' .
90 8
                      ')' .
91 8
                      ' VALUES' .
92 8
                      ' (' .
93 8
                      ' :date' .
94 8
                      ', :connection' .
95 8
                      ', :hits' .
96 8
                      ', :misses' .
97 8
                      ', :created_at' .
98 8
                      ', :updated_at' .
99 8
                      ')';
100
101 8
                    $this->doUpsertStatement($sql);
102 8
                }
103 8
                break;
104
105
            default:
106
                throw new \ErrorException(
107
                    __CLASS__.'::'.__FUNCTION__.
108
                    ' Driver: ' . $driver . ' not supported.'
109
                );
110 8
        }
111 8
    }
112
113
    /**
114
     * Helper. avoid duplicate code
115
     * Run sqlite3 update statement.
116
     * which must have same number of params
117
     * as sql bindings.
118
     *
119
     * @param string $sql
120
     * @return void
121
     */
122 8 View Code Duplication
    protected function doUpdateStatement($sql)
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...
123
    {
124 8
        $now = \Carbon\Carbon::now()->toDateTimeString();
125
126 8
        DB::statement($sql, [
127 8
            'date' => $this->argument('date'),
128 8
            'connection' => $this->argument('connection'),
129 8
            'hits' =>  $this->argument('hits'),
130 8
            'misses' =>  $this->argument('misses'),
131
            'updated_at' => $now
132 8
        ]);
133 8
    }
134
135
    /**
136
     * Helper. avoid duplicate code
137
     * Run insert or update statement.
138
     *
139
     * @param string $sql
140
     * @return void
141
     */
142 8 View Code Duplication
    protected function doUpsertStatement($sql)
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...
143
    {
144 8
        $now = \Carbon\Carbon::now()->toDateTimeString();
145
146 8
        DB::statement($sql, [
147 8
            'date' => $this->argument('date'),
148 8
            'connection' => $this->argument('connection'),
149 8
            'hits' =>  $this->argument('hits'),
150 8
            'misses' =>  $this->argument('misses'),
151 8
            'created_at' => $now,
152
            'updated_at' => $now
153 8
        ]);
154 8
    }
155
156
    /**
157
     * Get the status of a sqlite3 UPDATE operation
158
     * from native stdClass
159
     *
160
     *
161
     * @param array $changes e.g:
162
     *     [ 0 => { "changes()": 1 }]
163
     * @return boolean
164
     */
165 8
    protected function sqliteUpdateDidChange(array $changes)
0 ignored issues
show
Coding Style introduced by
function sqliteUpdateDidChange() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
166
    {
167 8
        $status = 'changes()';
168 8
        return $changes[0]->$status;
169
    }
170
}
0 ignored issues
show
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...
171