Connector   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 75
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A cleanup() 0 4 1
A identify() 0 3 1
A created() 0 26 4
1
<?php
2
/**
3
 * Managed instance creator
4
 * User: moyo
5
 * Date: 09/08/2017
6
 * Time: 5:43 PM
7
 */
8
9
namespace Carno\Pool;
10
11
use function Carno\Coroutine\async;
12
use Carno\Pool\Exception\InvalidConnectorException;
13
use Carno\Promise\Promise;
14
use Carno\Promise\Promised;
15
use Closure;
16
use Generator;
17
18
class Connector
19
{
20
    /**
21
     * @var Pool
22
     */
23
    private $pool = null;
24
25
    /**
26
     * @var Closure
27
     */
28
    private $dialer = null;
29
30
    /**
31
     * @var string
32
     */
33
    private $identify = null;
34
35
    /**
36
     * Connector constructor.
37
     * @param Pool $pool
38
     * @param Closure $dialer
39
     * @param string $identify
40
     */
41
    public function __construct(Pool $pool, Closure $dialer, string $identify)
42
    {
43
        $this->pool = $pool;
44
        $this->dialer = $dialer;
45
        $this->identify = $identify;
46
    }
47
48
    /**
49
     */
50
    public function cleanup() : void
51
    {
52
        $this->pool = null;
53
        $this->dialer = null;
54
    }
55
56
    /**
57
     * @return string
58
     */
59
    public function identify() : string
60
    {
61
        return $this->identify;
62
    }
63
64
    /**
65
     * @return Promised|Poolable
66
     */
67
    public function created() : Promised
68
    {
69
        $created = ($this->dialer)();
70
71
        if ($created instanceof Generator) {
72
            $created = async($created);
73
            goto PROMISED;
74
        }
75
76
        if ($created instanceof Poolable) {
77
            // currently usable instance
78
            $created->pool($this->pool);
79
            return Promise::resolved($created);
80
        }
81
82
        PROMISED:
83
84
        if ($created instanceof Promised) {
85
            // deferred create instance
86
            $created->then(function (Poolable $created) {
87
                $created->pool($this->pool);
88
            });
89
            return $created;
90
        }
91
92
        throw new InvalidConnectorException($this->identify());
93
    }
94
}
95