Managed   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 103
rs 10
c 0
b 0
f 0
wmc 14

7 Methods

Rating   Name   Duplication   Size   Complexity  
A schedule() 0 3 1
A scheduled() 0 7 3
A release() 0 4 3
A cid() 0 3 2
A pool() 0 3 1
A closed() 0 13 2
A destroy() 0 7 2
1
<?php
2
/**
3
 * Conn managed
4
 * User: moyo
5
 * Date: 09/08/2017
6
 * Time: 3:37 PM
7
 */
8
9
namespace Carno\Pool;
10
11
use Carno\Promise\Promise;
12
use Carno\Promise\Promised;
13
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
80
    {
81
        $this->schedules[$evk] = $future;
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
111
    {
112
        if (($w = $this->schedules[Poolable::RELEASED] ?? null) && $w->pended()) {
113
            $w->resolve($this);
114
            return true;
115
        }
116
        return false;
117
    }
118
}
119