Allocation::releaseAll()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
namespace ResourcePool;
3
4
/**
5
 * @author Josh Di Fabio <[email protected]>
6
 *
7
 * @api
8
 */
9
class Allocation
10
{
11
    private $releaseFn;
12
    private $size;
13
    
14
    public function __construct($releaseFn, $size)
15
    {
16
        $this->releaseFn = $releaseFn;
17
        $this->size = $size;
18
    }
19
20
    /**
21
     * Releases a single resource from this allocation
22
     */
23
    public function releaseOne()
24
    {
25
        $this->release(1);
26
    }
27
28
    /**
29
     * Releases all resources from this allocation
30
     */
31
    public function releaseAll()
32
    {
33
        if ($this->size) {
34
            $this->release($this->size);
35
        }
36
    }
37
38
    /**
39
     * Releases the specified number of resources from this allocation
40
     *
41
     * @param int $count
42
     */
43
    public function release($count)
44
    {
45
        $count = min($count, $this->size);
46
47
        if (!$count) {
48
            return;
49
        }
50
51
        $this->size -= $count;
52
        call_user_func($this->releaseFn, $count);
53
    }
54
}
55