Total Complexity | 6 |
Total Lines | 44 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
23 | class Pool implements PoolInterface, Countable |
||
24 | { |
||
25 | use CloneLess; |
||
26 | |||
27 | private $serviceLocator; |
||
28 | |||
29 | private $busyOnes = []; |
||
30 | |||
31 | private $availableOnes = []; |
||
32 | |||
33 | public function __construct() |
||
34 | { |
||
35 | $this->serviceLocator = new ServiceLocator(); |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * @inheritDoc |
||
40 | */ |
||
41 | public function get($class): WorkerInterface |
||
42 | { |
||
43 | $instance = count($this->availableOnes) == 0 ? $this->serviceLocator->create($class) : array_pop($this->availableOnes); |
||
44 | $this->busyOnes[spl_object_hash($instance)] = $instance; |
||
45 | return $instance; |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * @inheritDoc |
||
50 | */ |
||
51 | public function free(WorkerInterface $worker) |
||
52 | { |
||
53 | $key = spl_object_hash($worker); |
||
54 | |||
55 | if (isset($this->busyOnes[$key])) { |
||
56 | unset($this->busyOnes[$key]); |
||
57 | $this->availableOnes[$key] = $worker; |
||
58 | } |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @return int |
||
63 | */ |
||
64 | public function count(): int |
||
67 | } |
||
68 | } |
||
69 |