Signals::joined()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * State signals
4
 * User: moyo
5
 * Date: 27/10/2017
6
 * Time: 4:02 PM
7
 */
8
9
namespace Carno\Cluster\Managed;
10
11
use Carno\Promise\Promise;
12
use Carno\Promise\Promised;
13
14
trait Signals
15
{
16
    /**
17
     * @var Promised[]
18
     */
19
    private $signals = [];
20
21
    /**
22
     * @return Promised
23
     */
24
    public function ready() : Promised
25
    {
26
        return $this->sigs(__METHOD__, ...func_get_args());
0 ignored issues
show
Bug introduced by
func_get_args() is expanded, but the parameter $act of Carno\Cluster\Managed\Signals::sigs() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

26
        return $this->sigs(__METHOD__, /** @scrutinizer ignore-type */ ...func_get_args());
Loading history...
27
    }
28
29
    /**
30
     * @return Promised
31
     */
32
    public function joined() : Promised
33
    {
34
        return $this->sigs(__METHOD__, ...func_get_args());
0 ignored issues
show
Bug introduced by
func_get_args() is expanded, but the parameter $act of Carno\Cluster\Managed\Signals::sigs() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

34
        return $this->sigs(__METHOD__, /** @scrutinizer ignore-type */ ...func_get_args());
Loading history...
35
    }
36
37
    /**
38
     * @param string $name
39
     * @param mixed $act
40
     * @return Promised
41
     */
42
    private function sigs(string $name, $act = null) : Promised
43
    {
44
        if (is_null($act)) {
45
            return $this->signals[$name] ?? $this->signals[$name] = Promise::deferred();
46
        }
47
48
        $state = $this->sigs($name);
49
50
        if (is_bool($act) && $act) {
51
            $state->pended() && $state->resolve();
52
        } elseif (is_callable($act)) {
53
            $state->then($act);
54
        }
55
56
        return $state;
57
    }
58
}
59