Completed
Push — master ( 0dd180...73bb90 )
by Tyler
02:24
created

License   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 242
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 23
lcom 2
cbo 2
dl 0
loc 242
rs 10
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A getModel() 0 17 2
A allocate() 0 13 3
A success() 0 4 1
A error() 0 4 1
A remaining() 0 4 1
A maximum() 0 4 1
A message() 0 4 1
name() 0 1 ?
used() 0 1 ?
adding() 0 1 ?
added() 0 1 ?
subtracting() 0 1 ?
subtracted() 0 1 ?
A add() 0 15 3
A sub() 0 19 4
A set() 0 14 4
1
<?php
2
3
namespace Tylercd100\License;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Tylercd100\License\Exceptions\LicenseException;
7
use Tylercd100\License\Models\License as LicenseModel;
8
use Tylercd100\License\HasLicenses;
9
10
abstract class License
11
{
12
    /**
13
     * Owner of the licenses
14
     *
15
     * @var Model
16
     */
17
    protected $owner;
18
19
    /**
20
     * The database model for tracking licenses
21
     *
22
     * @var LicenseModel
23
     */
24
    protected $model;
25
26
    /**
27
     * The default starting amount for the license
28
     *
29
     * @var int
30
     */
31
    protected $default = 0;
32
33
    function __construct(Model $owner)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
34
    {
35
        if (!in_array(HasLicenses::class, class_uses($owner))) {
36
            throw new LicenseException("The owner must use the trait: ".HasLicenses::class);
37
        }
38
39
        $this->model = $this->getModel($owner);
40
        $this->owner = $owner;
41
    }
42
43
    protected function getModel(Model $owner)
44
    {
45
        $opts = [
46
            "owner_type" => get_class($owner),
47
            "owner_id" => $owner->id,
48
            "license" => get_class($this),
49
        ];
50
51
        $x = LicenseModel::where($opts)->first();
52
        
53
        if (!$x) {
54
            $opts["quantity"] = $this->default;
55
            $x = LicenseModel::create($opts);
0 ignored issues
show
Bug introduced by
The method create() does not exist on Tylercd100\License\Models\License. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
56
        }
57
58
        return $x;
59
    }
60
61
    /**
62
     * Throws exception if there are not enough licenses available
63
     *
64
     * @param int $quantity
65
     * @param boolean $add
66
     * @return void
67
     */
68
    final public function allocate($quantity, $add = false)
69
    {
70
        $remaining = $this->remaining();
71
        if ($remaining < $quantity) {
72
            if(!$add) {
73
                $this->error($remaining, $quantity);
74
            } else {
75
                $this->add($quantity - $remaining);
76
            }
77
        }
78
79
        $this->success($remaining, $quantity);
0 ignored issues
show
Unused Code introduced by
The call to License::success() has too many arguments starting with $quantity.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
80
    }
81
    
82
    /**
83
     * Called when there are enough licenses available
84
     *
85
     * @param int $quantity
86
     * @return void
87
     */
88
    protected function success($quantity)
0 ignored issues
show
Unused Code introduced by
The parameter $quantity is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
89
    {
90
        
91
    }
92
93
    /**
94
     * Called when there are not enough licenses available
95
     *
96
     * @param int $remaining
97
     * @param int $quantity
98
     * @return void
99
     */
100
    protected function error($remaining, $quantity)
101
    {
102
        throw new LicenseException($this->message($remaining, $quantity));
103
    }
104
105
    /**
106
     * Returns the difference between the maximum amount licenses and what you are trying to limit
107
     *
108
     * @return int
109
     */
110
    public function remaining()
111
    {
112
        return max($this->maximum() - $this->used(), 0);
113
    }
114
115
    /**
116
     * Returns the maximum amount of licenses
117
     *
118
     * @return int
119
     */
120
    public function maximum()
121
    {
122
        return $this->model->quantity;
0 ignored issues
show
Documentation introduced by
The property quantity does not exist on object<Tylercd100\License\Models\License>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
123
    }
124
125
    /**
126
     * Returns the human readable error string when there are not enough licenses available.
127
     *
128
     * @param int $remaining Number of licenses available.
129
     * @param int $quantity Number of licenses trying to allocate.
130
     * @return string
131
     */
132
    protected function message($remaining, $quantity)
133
    {
134
        return "There are not enough licenses available. Tried to allocate {$quantity} but there are only {$remaining} available.";
135
    }
136
137
    /**
138
     * Returns human readable string for this license
139
     *
140
     * @return string
141
     */
142
    abstract public function name();
143
144
    /**
145
     * Returns the current amount of licenses in use
146
     *
147
     * @return int
148
     */
149
    abstract public function used();
150
151
    /**
152
     * Called before adding to the license count.
153
     * 
154
     * @param int $quantity
155
     * @return void
156
     */
157
    abstract protected function adding($quantity);
158
159
    /**
160
     * Called after adding to the license count
161
     *
162
     * @param int $quantity
163
     * @return void
164
     */
165
    abstract protected function added($quantity);
166
167
    /**
168
     * Called before subtracting the license count
169
     *
170
     * @param int $quantity
171
     * @return void
172
     */
173
    abstract protected function subtracting($quantity);
174
175
    /**
176
     * Called after subtracting the license count
177
     * 
178
     * @param int $quantity
179
     * @return void
180
     */
181
    abstract protected function subtracted($quantity);
182
183
    /**
184
     * Add more licenses
185
     *
186
     * @param int $quantity
187
     * @return boolean
188
     */
189
    final public function add($quantity = 1)
190
    {
191
        if (!is_int($quantity) || $quantity < 0) {
192
            throw new LicenseException("Quantity must be a positive integer.");
193
        }
194
195
        $this->adding($quantity);
196
197
        $this->model->quantity += $quantity;
0 ignored issues
show
Documentation introduced by
The property quantity does not exist on object<Tylercd100\License\Models\License>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
198
        $this->model->save();
199
200
        $this->added($quantity);
201
        
202
        return true;
203
    }
204
205
    /**
206
     * Subtract licenses
207
     *
208
     * @param int $quantity
209
     * @return boolean
210
     */
211
    final public function sub($quantity = 1)
212
    {
213
        if (!is_int($quantity) || $quantity < 0) {
214
            throw new LicenseException("Quantity must be a positive integer.");
215
        }
216
217
        if ($this->model->quantity - $quantity < 0) {
0 ignored issues
show
Documentation introduced by
The property quantity does not exist on object<Tylercd100\License\Models\License>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
218
            throw new LicenseException("You cannot remove more licenses than you have available.");
219
        }
220
221
        $this->subtracting($quantity);
222
223
        $this->model->quantity -= $quantity;
0 ignored issues
show
Documentation introduced by
The property quantity does not exist on object<Tylercd100\License\Models\License>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
224
        $this->model->save();
225
226
        $this->subtracted($quantity);
227
        
228
        return true;
229
    }
230
231
    /**
232
     * Set the amount of licenses
233
     *
234
     * @param int $quantity
235
     * @return boolean
236
     */
237
    final public function set($quantity)
238
    {
239
        if (!is_int($quantity) || $quantity < 0) {
240
            throw new LicenseException("Quantity must be a positive integer.");
241
        }
242
243
        $difference = $quantity - $this->maximum();
244
245
        if ($difference < 0) {
246
            return $this->sub(abs($difference));
247
        } else {
248
            return $this->add($difference);
249
        }
250
    }
251
}
252