Completed
Push — master ( c5ae65...0dd180 )
by Tyler
02:59
created

License::success()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
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->owner = $owner;
40
        $this->model = LicenseModel::firstOrCreate([
41
            "owner_type" => get_class($owner),
42
            "owner_id" => $owner->id,
43
            "license" => get_class($this),
44
        ], [
45
            "quantity" => $this->default,
46
        ]);
47
    }
48
49
    /**
50
     * Throws exception if there are not enough licenses available
51
     *
52
     * @param int $quantity
53
     * @param boolean $add
54
     * @return void
55
     */
56
    final public function allocate($quantity, $add = false)
57
    {
58
        $remaining = $this->remaining();
59
        if ($remaining < $quantity) {
60
            if(!$add) {
61
                $this->error($remaining, $quantity);
62
            } else {
63
                $this->add($quantity - $remaining);
64
            }
65
        }
66
67
        $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...
68
    }
69
    
70
    /**
71
     * Called when there are enough licenses available
72
     *
73
     * @param int $quantity
74
     * @return void
75
     */
76
    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...
77
    {
78
        
79
    }
80
81
    /**
82
     * Called when there are not enough licenses available
83
     *
84
     * @param int $remaining
85
     * @param int $quantity
86
     * @return void
87
     */
88
    protected function error($remaining, $quantity)
89
    {
90
        throw new LicenseException($this->message($remaining, $quantity));
91
    }
92
93
    /**
94
     * Returns the difference between the maximum amount licenses and what you are trying to limit
95
     *
96
     * @return int
97
     */
98
    public function remaining()
99
    {
100
        return max($this->maximum() - $this->used(), 0);
101
    }
102
103
    /**
104
     * Returns the maximum amount of licenses
105
     *
106
     * @return int
107
     */
108
    public function maximum()
109
    {
110
        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...
111
    }
112
113
    /**
114
     * Returns the human readable error string when there are not enough licenses available.
115
     *
116
     * @param int $remaining Number of licenses available.
117
     * @param int $quantity Number of licenses trying to allocate.
118
     * @return string
119
     */
120
    protected function message($remaining, $quantity)
121
    {
122
        return "There are not enough licenses available. Tried to allocate {$quantity} but there are only {$remaining} available.";
123
    }
124
125
    /**
126
     * Returns human readable string for this license
127
     *
128
     * @return string
129
     */
130
    abstract public function name();
131
132
    /**
133
     * Returns the current amount of licenses in use
134
     *
135
     * @return int
136
     */
137
    abstract public function used();
138
139
    /**
140
     * Called before adding to the license count.
141
     * 
142
     * @param int $quantity
143
     * @return void
144
     */
145
    abstract protected function adding($quantity);
146
147
    /**
148
     * Called after adding to the license count
149
     *
150
     * @param int $quantity
151
     * @return void
152
     */
153
    abstract protected function added($quantity);
154
155
    /**
156
     * Called before subtracting the license count
157
     *
158
     * @param int $quantity
159
     * @return void
160
     */
161
    abstract protected function subtracting($quantity);
162
163
    /**
164
     * Called after subtracting the license count
165
     * 
166
     * @param int $quantity
167
     * @return void
168
     */
169
    abstract protected function subtracted($quantity);
170
171
    /**
172
     * Add more licenses
173
     *
174
     * @param int $quantity
175
     * @return boolean
176
     */
177
    final public function add($quantity = 1)
178
    {
179
        if (!is_int($quantity) || $quantity < 0) {
180
            throw new LicenseException("Quantity must be a positive integer.");
181
        }
182
183
        $this->adding($quantity);
184
185
        $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...
186
        $this->model->save();
187
188
        $this->added($quantity);
189
        
190
        return true;
191
    }
192
193
    /**
194
     * Subtract licenses
195
     *
196
     * @param int $quantity
197
     * @return boolean
198
     */
199
    final public function sub($quantity = 1)
200
    {
201
        if (!is_int($quantity) || $quantity < 0) {
202
            throw new LicenseException("Quantity must be a positive integer.");
203
        }
204
205
        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...
206
            throw new LicenseException("You cannot remove more licenses than you have available.");
207
        }
208
209
        $this->subtracting($quantity);
210
211
        $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...
212
        $this->model->save();
213
214
        $this->subtracted($quantity);
215
        
216
        return true;
217
    }
218
219
    /**
220
     * Set the amount of licenses
221
     *
222
     * @param int $quantity
223
     * @return boolean
224
     */
225
    final public function set($quantity)
226
    {
227
        if (!is_int($quantity) || $quantity < 0) {
228
            throw new LicenseException("Quantity must be a positive integer.");
229
        }
230
231
        $difference = $quantity - $this->maximum();
232
233
        if ($difference < 0) {
234
            return $this->sub(abs($difference));
235
        } else {
236
            return $this->add($difference);
237
        }
238
    }
239
}
240