| Total Complexity | 14 |
| Total Lines | 103 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 14 | trait Managed |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | private $cid = 'c--'; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var Pool |
||
| 23 | */ |
||
| 24 | private $pool = null; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var Promised |
||
| 28 | */ |
||
| 29 | private $closed = null; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var Promised[] |
||
| 33 | */ |
||
| 34 | private $schedules = []; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @see Poolable::cid |
||
| 38 | * @param string $set |
||
| 39 | * @return string |
||
| 40 | */ |
||
| 41 | final public function cid(string $set = null) : string |
||
| 42 | { |
||
| 43 | return $set ? $this->cid = $set : $this->cid; |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @see Poolable::pool |
||
| 48 | * @param Pool $pool |
||
| 49 | */ |
||
| 50 | final public function pool(Pool $pool) : void |
||
| 51 | { |
||
| 52 | $this->pool = $pool; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @see Poolable::closed |
||
| 57 | * @return Promised |
||
| 58 | */ |
||
| 59 | final public function closed() : Promised |
||
| 60 | { |
||
| 61 | if (isset($this->closed)) { |
||
| 62 | return $this->closed; |
||
| 63 | } |
||
| 64 | |||
| 65 | ($this->closed = Promise::deferred())->then(function () { |
||
| 66 | $this->destroy(); |
||
| 67 | }, function () { |
||
| 68 | $this->destroy(); |
||
| 69 | }); |
||
| 70 | |||
| 71 | return $this->closed; |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @see Poolable::schedule |
||
| 76 | * @param int $evk |
||
| 77 | * @param Promised $future |
||
| 78 | */ |
||
| 79 | final public function schedule(int $evk, Promised $future) : void |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @see Poolable::release |
||
| 86 | */ |
||
| 87 | final public function release() : void |
||
| 88 | { |
||
| 89 | if (isset($this->pool)) { |
||
| 90 | $this->scheduled() || $this->pool->recycle($this); |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @see Poolable::destroy |
||
| 96 | */ |
||
| 97 | final public function destroy() : void |
||
| 98 | { |
||
| 99 | if (isset($this->pool)) { |
||
| 100 | $pool = $this->pool; |
||
| 101 | unset($this->pool); |
||
| 102 | $this->scheduled(); |
||
| 103 | $pool->release($this); |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @return bool |
||
| 109 | */ |
||
| 110 | final private function scheduled() : bool |
||
| 117 | } |
||
| 118 | } |
||
| 119 |