SAR   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 38
rs 10
c 0
b 0
f 0
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A sarRun() 0 29 5
1
<?php
2
/**
3
 * (S)elect (A)nd (R)elease
4
 * User: moyo
5
 * Date: 02/11/2017
6
 * Time: 6:10 PM
7
 */
8
9
namespace Carno\Pool\Wrapper;
10
11
use function Carno\Coroutine\defer;
12
use Carno\Pool\Contracts\Broken;
13
use Carno\Pool\Pool;
14
use Carno\Pool\Poolable;
15
use Carno\Promise\Promised;
16
17
trait SAR
18
{
19
    /**
20
     * @param Pool $pool
21
     * @param string $method
22
     * @param array $arguments
23
     * @param Promised $interrupter
24
     * @return mixed
25
     */
26
    protected function sarRun(Pool $pool, string $method, array $arguments, Promised $interrupter = null)
27
    {
28
        /**
29
         * @var Poolable $conn
30
         */
31
        $conn = null;
32
33
        // MUST use defer because it can exec whatever job be FIN or KILL
34
        // otherwise conn will never be released
35
        yield defer(function ($stage) use (&$conn) {
36
            if ($conn instanceof Poolable) {
37
                $stage instanceof Broken ? $conn->destroy() : $conn->release();
38
            } elseif ($stage instanceof Poolable) {
39
                $stage->release();
40
            }
41
        });
42
43
        // pick out
44
        $conn = yield $pool->select();
45
46
        // check interrupter
47
        if ($interrupter) {
48
            $interrupter->then(function () use ($conn) {
49
                return $conn;
50
            });
51
        }
52
53
        // execute
54
        return yield $conn->$method(...$arguments);
55
    }
56
}
57