Completed
Push — master ( 6d8e2b...e8f84b )
by Tyler
07:19
created

License   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 252
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 24
lcom 2
cbo 2
dl 0
loc 252
rs 10
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A getModel() 0 17 2
A getOwner() 0 4 1
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
     * Returns the owner of the license
63
     *
64
     * @return Model
65
     */
66
    public function getOwner()
67
    {
68
        return $this->owner;
69
    }
70
71
    /**
72
     * Throws exception if there are not enough licenses available
73
     *
74
     * @param int $quantity
75
     * @param boolean $add
76
     * @return void
77
     */
78
    final public function allocate($quantity, $add = false)
79
    {
80
        $remaining = $this->remaining();
81
        if ($remaining < $quantity) {
82
            if(!$add) {
83
                $this->error($remaining, $quantity);
84
            } else {
85
                $this->add($quantity - $remaining);
86
            }
87
        }
88
89
        $this->success($quantity);
90
    }
91
    
92
    /**
93
     * Called when there are enough licenses available
94
     *
95
     * @param int $quantity
96
     * @return void
97
     */
98
    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...
99
    {
100
        
101
    }
102
103
    /**
104
     * Called when there are not enough licenses available
105
     *
106
     * @param int $remaining
107
     * @param int $quantity
108
     * @return void
109
     */
110
    protected function error($remaining, $quantity)
111
    {
112
        throw new LicenseException($this->message($remaining, $quantity));
113
    }
114
115
    /**
116
     * Returns the difference between the maximum amount licenses and what you are trying to limit
117
     *
118
     * @return int
119
     */
120
    public function remaining()
121
    {
122
        return max($this->maximum() - $this->used(), 0);
123
    }
124
125
    /**
126
     * Returns the maximum amount of licenses
127
     *
128
     * @return int
129
     */
130
    public function maximum()
131
    {
132
        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...
133
    }
134
135
    /**
136
     * Returns the human readable error string when there are not enough licenses available.
137
     *
138
     * @param int $remaining Number of licenses available.
139
     * @param int $quantity Number of licenses trying to allocate.
140
     * @return string
141
     */
142
    protected function message($remaining, $quantity)
143
    {
144
        return "There are not enough licenses available. Tried to allocate {$quantity} but there are only {$remaining} available.";
145
    }
146
147
    /**
148
     * Returns human readable string for this license
149
     *
150
     * @return string
151
     */
152
    abstract public function name();
153
154
    /**
155
     * Returns the current amount of licenses in use
156
     *
157
     * @return int
158
     */
159
    abstract public function used();
160
161
    /**
162
     * Called before adding to the license count.
163
     * 
164
     * @param int $quantity
165
     * @return void
166
     */
167
    abstract protected function adding($quantity);
168
169
    /**
170
     * Called after adding to the license count
171
     *
172
     * @param int $quantity
173
     * @return void
174
     */
175
    abstract protected function added($quantity);
176
177
    /**
178
     * Called before subtracting the license count
179
     *
180
     * @param int $quantity
181
     * @return void
182
     */
183
    abstract protected function subtracting($quantity);
184
185
    /**
186
     * Called after subtracting the license count
187
     * 
188
     * @param int $quantity
189
     * @return void
190
     */
191
    abstract protected function subtracted($quantity);
192
193
    /**
194
     * Add more licenses
195
     *
196
     * @param int $quantity
197
     * @return boolean
198
     */
199
    final public function add($quantity = 1)
200
    {
201
        if (!is_int($quantity) || $quantity < 0) {
202
            throw new LicenseException("Quantity must be a positive integer.");
203
        }
204
205
        $this->adding($quantity);
206
207
        $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...
208
        $this->model->save();
209
210
        $this->added($quantity);
211
        
212
        return true;
213
    }
214
215
    /**
216
     * Subtract licenses
217
     *
218
     * @param int $quantity
219
     * @return boolean
220
     */
221
    final public function sub($quantity = 1)
222
    {
223
        if (!is_int($quantity) || $quantity < 0) {
224
            throw new LicenseException("Quantity must be a positive integer.");
225
        }
226
227
        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...
228
            throw new LicenseException("You cannot remove more licenses than you have available.");
229
        }
230
231
        $this->subtracting($quantity);
232
233
        $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...
234
        $this->model->save();
235
236
        $this->subtracted($quantity);
237
        
238
        return true;
239
    }
240
241
    /**
242
     * Set the amount of licenses
243
     *
244
     * @param int $quantity
245
     * @return boolean
246
     */
247
    final public function set($quantity)
248
    {
249
        if (!is_int($quantity) || $quantity < 0) {
250
            throw new LicenseException("Quantity must be a positive integer.");
251
        }
252
253
        $difference = $quantity - $this->maximum();
254
255
        if ($difference < 0) {
256
            return $this->sub(abs($difference));
257
        } else {
258
            return $this->add($difference);
259
        }
260
    }
261
}
262