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

Quota::validPeriods()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
ccs 3
cts 3
cp 1
cc 1
eloc 3
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;
13
14
use bandwidthThrottle\tokenBucket\Rate;
15
use Projectmentor\Quota\Contracts\Quota as QuotaInterface;
16
17
/**
18
 * This is the Quota class.
19
 *
20
 * NOTE: While currently implemented as a
21
 * concrete class for testing. This class
22
 * should be used only for inheritance.
23
 */
24
class Quota implements QuotaInterface
25
{
26
    /**
27
     * The configured connection name.
28
     *
29
     * @var string
30
     */
31
    protected $connection;
32
33
    /**
34
     * The configured connection index.
35
     *
36
     * @var string
37
     */
38
    protected $index;
39
40
    /**
41
     * The quota limit
42
     *
43
     * @var int
44
     */
45
    protected $limit;
46
47
    /**
48
     * The period to limit
49
     *
50
     * @var string
51
     */
52
    protected $period;
53
54
    /**
55
     *  Construct instance.
56
     *
57
     *  @param string $connection
58
     *  @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...
59
     */
60 14
    public function __construct($connection)
61
    {
62 14
        $this->connection = $connection;
63 14
        $this->index = 'quota.connections.' . $connection;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 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...
64
65 14
        $this->limit = config($this->index . '.limit');
66
67 14
        $period = config($this->index . '.period');
68 14
        $this->setPeriod($period);
69 14
    }
70
71
    /**
72
     * Get a list of valid periods.
73
     * @See  bandwidthThrottle\tokenBucket\Rate::CONSTANTS
74
     *
75
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,integer|double|string|boolean>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
76
     */
77 14
    public function validPeriods()
78
    {
79 14
        $r = new \ReflectionClass(Rate::class);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $r. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
80
        //print_r($r->getConstants());
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
81 14
        return $r->getConstants();
82
    }
83
84
    /**
85
     * Validate period against Rate::CONSTANT values
86
     *
87
     * @param string $period one of:
88
     *        bandwidthThrottle\tokenBucket\Rate::CONSTANTS
89
     * @return boolean true on success
90
     */
91 14
    public function validatePeriod($period)
0 ignored issues
show
Coding Style introduced by
function validatePeriod() 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...
92
    {
93 14
        $result = false;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 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...
94 14
        $constants = $this->validPeriods();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 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...
95 14
        $values = array_values($constants);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 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...
96 14
        if(in_array($period, $values))
97 14
            $result = true;
98 14
        return $result;
99
    }
100
101
   /**
102
    * Public interface.
103
    *
104
    * NOTE: this class and method are really abstract,
105
    * but we leave it instantiable for easier testing. 
106
    */ 
107 1
    public function enforce()
108
    {
109
        //Override this in descendants.
110 1
        throw new \Exception('Not implemented here.');
111
    }
112
113
    /**
114
     * Get the connection name.
115
     *
116
     * @return string
117
     */
118 9
    public function getConnection()
119
    {
120 9
        return $this->connection;
121
    }
122
123
    /**
124
     * Get the index for the connection
125
     *
126
     * @return string
127
     */
128 3
    public function getIndex()
129
    {
130 3
        return $this->index;
131
    }
132
133
    /**
134
     * Get the limit
135
     *
136
     * @return int
137
     */
138 5
    public function getLimit()
139
    {
140 5
        return $this->limit;
141
    }
142
143
    /**
144
     * Get the period.
145
     *
146
     * @return string \bandwidthThrottle\tokenBucket\Rate::CONSTANT
147
     */
148 3
    public function getPeriod()
149
    {
150 3
        return $this->period;
151
    }
152
153
    /**
154
     * Set the connection name.
155
     *
156
     * @param string $connection
157
     * @return void
158
     */
159
    public function setConnection($connection)
160
    {
161
        $this->connection = $connection;
162
    }
163
164
    /**
165
     * Set the index
166
     *
167
     * @param string $index
168
     * @return void
169
     */
170
    public function setIndex($index)
171
    {
172
        $this->index = $index;
173
    }
174
175
    /**
176
     * Set the limit
177
     *
178
     * @param  int $limit
179
     * @return void
180
     */
181
    public function setLimit($limit)
182
    {
183
        $this->limit = $limit;
184
    }
185
186
    /**
187
     * Set the period.
188
     *
189
     * @param string \bandwidthThrottle\tokenBucket\Rate::CONSTANT
190
     * @return void
191
     * @throws InvalidArgumentException
192
     * 
193
     */
194 14
    public function setPeriod($period)
195
    {
196 14
        if(! $this->validatePeriod($period))
197 14
        {
198
            $expected = $this->validPeriods();
199
200
            throw new \InvalidArgumentException(
201
                __CLASS__.'::'.__FUNCTION__.
202
                ' Invalid period: ' . $period . 
203
                ' in connection configuration: ' . $this->connection .
204
                ' expected one of: ' . PHP_EOL . print_r($expected,1));
205
        }
206
207 14
        $this->period = $period;
208 14
    }
209
}
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...
210