Completed
Push — master ( abfecd...ad8f04 )
by Stéphane
04:22
created

ReactPromiseAdapter::getState()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Http\Adapter;
4
5
use React\EventLoop\LoopInterface;
6
use React\Promise\PromiseInterface as ReactPromise;
7
use Http\Client\Promise;
8
use Http\Client\Exception;
9
use Psr\Http\Message\ResponseInterface;
10
11
/**
12
 * React promise adapter implementation
13
 * @author Stéphane Hulard <[email protected]>
14
 */
15
class ReactPromiseAdapter implements Promise
16
{
17
    /**
18
     * Promise status
19
     * @var string
20
     */
21
    private $state = Promise::PENDING;
22
23
    /**
24
     * Adapted React promise
25
     * @var ReactPromise
26
     */
27
    private $promise;
28
29
    /**
30
     * PSR7 received response
31
     * @var ResponseInterface
32
     */
33
    private $response;
34
35
    /**
36
     * Execution error
37
     * @var Exception
38
     */
39
    private $exception;
40
41
    /**
42
     * React Event Loop used for synchronous processing
43
     * @var LoopInterface
44
     */
45
    private $loop;
46
47
    /**
48
     * Initialize the promise
49
     * @param ReactPromise $promise
50
     */
51 108
    public function __construct(ReactPromise $promise)
52
    {
53 108
        $promise->then(
54 108
            function(ResponseInterface $response) {
55 105
                $this->state = Promise::FULFILLED;
56 105
                $this->response = $response;
57 108
            },
58 3
            function(Exception $error) {
59 3
                $this->state = Promise::REJECTED;
60 3
                $this->exception = $error;
61 3
            }
62 108
        );
63 108
        $this->promise = $promise;
64 108
    }
65
66
    /**
67
     * Allow to apply callable when the promise resolve
68
     * @param  callable|null $onFulfilled
69
     * @param  callable|null $onRejected
70
     * @return ReactPromiseAdapter
71
     */
72 55
    public function then(callable $onFulfilled = null, callable $onRejected = null)
73
    {
74
        $this->promise->then(function() use ($onFulfilled) {
75 53
            if( null !== $onFulfilled ) {
76 53
                call_user_func($onFulfilled, $this->response);
77 53
            }
78 55
        }, function() use ($onRejected) {
79 2
            if( null !== $onRejected ) {
80 2
                call_user_func($onRejected, $this->exception);
81 2
            }
82 55
        });
83 55
        return $this;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 53
    public function getState()
90
    {
91 53
        return $this->state;
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97 52
    public function getResponse()
98
    {
99 52
        return $this->response;
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 1
    public function getException()
106
    {
107 1
        return $this->exception;
108
    }
109
110
    /**
111
     * Set EventLoop used for synchronous processing
112
     * @param LoopInterface $loop
113
     * @return ReactPromiseAdapter
114
     */
115 108
    public function setLoop(LoopInterface $loop)
116
    {
117 108
        $this->loop = $loop;
118 108
        return $this;
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124 108
    public function wait()
125
    {
126 108
        if( null === $this->loop ) {
127
            throw new \LogicException("You must set the loop before wait!");
128
        }
129 108
        $this->loop->run();
130 108
    }
131
}
132