Completed
Push — master ( b2a65d...63a169 )
by Kirill
02:18
created

Deferred::invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\SDL\Process;
11
12
/**
13
 * Class Deferred
14
 */
15
class Deferred implements DeferredInterface
16
{
17
    /**
18
     * @var ProcessInterface
19
     */
20
    private $process;
21
22
    /**
23
     * @var \Closure[]
24
     */
25
    private $then = [];
26
27
    /**
28
     * Deferred constructor.
29
     * @param ProcessInterface $process
30
     * @param \Closure $then
31
     */
32
    public function __construct(ProcessInterface $process, \Closure $then)
33
    {
34
        $this->process = $process;
35
        $this->then($then);
36
    }
37
38
    /**
39
     * @param callable $then
40
     * @return DeferredInterface
41
     */
42
    public function then(callable $then): DeferredInterface
43
    {
44
        $this->then[] = $then;
45
46
        return $this;
47
    }
48
49
    /**
50
     * @param mixed ...$args
51
     * @return mixed
52
     */
53
    public function __invoke(...$args)
54
    {
55
        return $this->invoke(...$args);
56
    }
57
58
    /**
59
     * @param mixed $value
60
     * @return mixed
61
     */
62
    public function invoke($value)
63
    {
64
        foreach ($this->then as $callback) {
65
            $value = $this->process->await($callback($value));
66
        }
67
68
        return $value;
69
    }
70
}
71