Completed
Push — master ( f77e88...54a4fc )
by David
03:40
created

Deferred::wait()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 14
cp 0
rs 9.7
c 0
b 0
f 0
cc 4
nc 6
nop 1
crap 20
1
<?php
2
3
namespace Http\Client\Common;
4
5
use Http\Client\Exception;
6
use Http\Promise\Promise;
7
use Psr\Http\Message\ResponseInterface;
8
9
/**
10
 * A deferred allow to return a promise which has not been resolved yet.
11
 */
12
class Deferred implements Promise
13
{
14
    private $value;
15
16
    private $failure;
17
18
    private $state;
19
20
    private $waitCallback;
21
22
    private $onFulfilledCallbacks;
23
24
    private $onRejectedCallbacks;
25
26
    public function __construct(callable $waitCallback)
27
    {
28
        $this->waitCallback = $waitCallback;
29
        $this->state = Promise::PENDING;
30
        $this->onFulfilledCallbacks = [];
31
        $this->onRejectedCallbacks = [];
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function then(callable $onFulfilled = null, callable $onRejected = null)
38
    {
39
        $deferred = new self($this->waitCallback);
40
41 View Code Duplication
        $this->onFulfilledCallbacks[] = function (ResponseInterface $response) use ($onFulfilled, $deferred) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
            try {
43
                if (null !== $onFulfilled) {
44
                    $response = $onFulfilled($response);
45
                }
46
                $deferred->resolve($response);
47
            } catch (Exception $exception) {
48
                $deferred->reject($exception);
49
            }
50
        };
51
52 View Code Duplication
        $this->onRejectedCallbacks[] = function (Exception $exception) use ($onRejected, $deferred) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
            try {
54
                if (null !== $onRejected) {
55
                    $response = $onRejected($exception);
56
                    $deferred->resolve($response);
57
58
                    return;
59
                }
60
                $deferred->reject($exception);
61
            } catch (Exception $newException) {
62
                $deferred->reject($newException);
63
            }
64
        };
65
66
        return $deferred;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function getState()
73
    {
74
        return $this->state;
75
    }
76
77
    /**
78
     * Resolve this deferred with a Response.
79
     */
80
    public function resolve(ResponseInterface $response)
81
    {
82
        if (self::PENDING !== $this->state) {
83
            return;
84
        }
85
86
        $this->value = $response;
87
        $this->state = self::FULFILLED;
88
89
        foreach ($this->onFulfilledCallbacks as $onFulfilledCallback) {
90
            $onFulfilledCallback($response);
91
        }
92
    }
93
94
    /**
95
     * Reject this deferred with an Exception.
96
     */
97
    public function reject(Exception $exception)
98
    {
99
        if (self::PENDING !== $this->state) {
100
            return;
101
        }
102
103
        $this->failure = $exception;
104
        $this->state = self::REJECTED;
105
106
        foreach ($this->onRejectedCallbacks as $onRejectedCallback) {
107
            $onRejectedCallback($exception);
108
        }
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    public function wait($unwrap = true)
115
    {
116
        if (self::PENDING === $this->state) {
117
            $callback = $this->waitCallback;
118
            $callback();
119
        }
120
121
        if (!$unwrap) {
122
            return;
123
        }
124
125
        if (self::FULFILLED === $this->state) {
126
            return $this->value;
127
        }
128
129
        throw $this->failure;
130
    }
131
}
132