Completed
Pull Request — master (#4)
by
unknown
07:52
created

Quota::getConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
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;
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 4 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();
95 14
        $values = array_values($constants);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 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
        }
99 14
        return $result;
100
    }
101
102
   /**
103
    * Public interface.
104
    *
105
    * NOTE: this class and method are really abstract,
106
    * but we leave it instantiable for easier testing.
107
    */
108 1
    public function enforce()
109
    {
110
        //Override this in descendants.
111 1
        throw new \Exception('Not implemented here.');
112
    }
113
114
    /**
115
     * Get the connection name.
116
     *
117
     * @return string
118
     */
119 9
    public function getConnection()
120
    {
121 9
        return $this->connection;
122
    }
123
124
    /**
125
     * Get the index for the connection
126
     *
127
     * @return string
128
     */
129 3
    public function getIndex()
130
    {
131 3
        return $this->index;
132
    }
133
134
    /**
135
     * Get the limit
136
     *
137
     * @return int
138
     */
139 5
    public function getLimit()
140
    {
141 5
        return $this->limit;
142
    }
143
144
    /**
145
     * Get the period.
146
     *
147
     * @return string \bandwidthThrottle\tokenBucket\Rate::CONSTANT
148
     */
149 3
    public function getPeriod()
150
    {
151 3
        return $this->period;
152
    }
153
154
    /**
155
     * Set the connection name.
156
     *
157
     * @param string $connection
158
     * @return void
159
     */
160
    public function setConnection($connection)
161
    {
162
        $this->connection = $connection;
163
    }
164
165
    /**
166
     * Set the index
167
     *
168
     * @param string $index
169
     * @return void
170
     */
171
    public function setIndex($index)
172
    {
173
        $this->index = $index;
174
    }
175
176
    /**
177
     * Set the limit
178
     *
179
     * @param  int $limit
180
     * @return void
181
     */
182
    public function setLimit($limit)
183
    {
184
        $this->limit = $limit;
185
    }
186
187
    /**
188
     * Set the period.
189
     *
190
     * @param string \bandwidthThrottle\tokenBucket\Rate::CONSTANT
191
     * @return void
192
     * @throws InvalidArgumentException
193
     *
194
     */
195 14
    public function setPeriod($period)
196
    {
197 14
        if (! $this->validatePeriod($period)) {
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
208 14
        $this->period = $period;
209 14
    }
210
}
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...
211