Test Setup Failed
Pull Request — master (#1)
by
unknown
06:50
created

ResetQuotaLog::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
cc 1
eloc 2
nc 1
nop 0
crap 1
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
        {
53 8
        case 'mysql':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
54
55
            $sql = 'INSERT INTO quotalog' .
56
                ' (date, connection, hits, misses, created_at, updated_at)' .
57
                ' VALUES' .
58
                ' ( :date, :connection, :hits, :misses, :created_at, :updated_at)' .
59
                ' ON DUPLICATE KEY UPDATE' .
60
                ' hits = VALUES(hits),' .
61
                ' misses = VALUES(misses),' .
62
                ' updated_at = VALUES(updated_at)';
63
64
            $this->doUpsertStatement($sql);
65
66
            break;
67 8
        case 'sqlite':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

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