Completed
Push — master ( e8f84b...de41c5 )
by Tyler
02:31
created

License::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

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