Completed
Push — master ( a135f2...bb2017 )
by Tyler
02:21
created

HasLicenses::licensesSub()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
3
namespace Tylercd100\License;
4
5
use Tylercd100\License\License;
6
7
trait HasLicenses
8
{
9
    /**
10
     * Attempt to allocate unused licenses.
11
     *
12
     * @param string $class The License class you want to work with
13
     * @param int $quantity The amount of licenses you want to attempt to use
14
     * @param boolean $add If true then it will increase the maximum available licenses
15
     * @return self
16
     */
17
    public function licensesAllocate($class, $quantity, $add = false)
18
    {
19
        $license = $this->getLicenseInstance($class);        
20
        $license->check($quantity, $add);
21
        return $this;
22
    }
23
24
    /**
25
     * Returns the amount of unused licenses.
26
     *
27
     * @param string $class The License class you want to work with
28
     * @return int
29
     */
30
    public function licensesRemaining($class)
31
    {
32
        $license = $this->getLicenseInstance($class);        
33
        return $license->remaining();
34
    }
35
36
    /**
37
     * Returns the amount of used licenses
38
     *
39
     * @param string $class The License class you want to work with
40
     * @return int
41
     */
42
    public function licensesUsed($class)
43
    {
44
        $license = $this->getLicenseInstance($class);        
45
        return $license->used();
46
    }
47
48
    /**
49
     * Increase the maximum amount of licenses
50
     *
51
     * @param string $class The License class you want to work with
52
     * @param integer $quantity
53
     * @return self
54
     */
55
    public function licensesAdd($class, $quantity = 1)
56
    {
57
        // Add quantity of licenses
58
        $license = $this->getLicenseInstance($class);
59
        $license->add($quantity);
60
        return $this;
61
    }
62
63
    /**
64
     * Decreases the maximum amount of licenses available
65
     *
66
     * @param string $class The License class you want to work with
67
     * @param integer $quantity
68
     * @return self
69
     */
70
    public function licensesSub($class, $quantity = 1)
71
    {
72
        // Subtract quantity of licenses
73
        $license = $this->getLicenseInstance($class);
74
        $license->sub($quantity);
75
        return $this;
76
    }
77
78
    /**
79
     * Sets the maximum amount of licenses to a specific value
80
     *
81
     * @param string $class The License class you want to work with
82
     * @param integer $quantity
83
     * @return self
84
     */
85
    public function licensesSet($class, $quantity)
86
    {
87
        // Set the quantity of licenses
88
        $license = $this->getLicenseInstance($class);
89
        $license->set($quantity);
90
        return $this;
91
    }
92
93
    /**
94
     * Creates an instance of License from the supplied classname string
95
     *
96
     * @param string $class The License class you want to work with
97
     * @return License
98
     */
99
    private function getLicenseInstance($class)
100
    {
101
        $license = new $class($this);
102
        if (!($license instanceof License)) {
103
            throw LicenseException("Expected ".get_class($license)." to be an instanceof ".License::class);
104
        }
105
        return $license;
106
    }
107
}
108