Completed
Push — master ( 450f6c...ad56b7 )
by Stefan
05:07
created

Stream::timeout()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Gemz\HttpClient;
4
5
use Symfony\Contracts\HttpClient\ChunkInterface;
6
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
7
use Symfony\Contracts\HttpClient\ResponseInterface;
8
9
class Stream
10
{
11
    /** @var string */
12
    const STATUS_FULFILLED = 'FULFILLED';
13
14
    /** @var string */
15
    const STATUS_REJECTED = 'REJECTED';
16
17
    /** @var string */
18
    const STATUS_TIMEOUT = 'TIMEOUT';
19
20
    /** @var string */
21
    const STATUS_PENDING = 'PENDING';
22
23
    /** @var ResponseInterface */
24
    protected $response;
25
26
    /** @var ChunkInterface */
27
    protected $chunk;
28
29
    /** @var string */
30
    protected $status = self::STATUS_PENDING;
31
32
    /** @var TransportExceptionInterface */
33
    protected $exception;
34
35
    /**
36
     * @param ResponseInterface $response
37
     * @param ChunkInterface $chunk
38
     *
39
     * @return Stream
40
     */
41
    public static function from(ResponseInterface $response, ChunkInterface $chunk)
42
    {
43
        return new self($response, $chunk);
44
    }
45
46
    /**
47
     * @param ResponseInterface $response
48
     * @param ChunkInterface $chunk
49
     */
50
    public function __construct(ResponseInterface $response, ChunkInterface $chunk)
51
    {
52
        $this->response = $response;
53
        $this->chunk = $chunk;
54
55
        $this->detectStatus();
56
    }
57
58
    protected function detectStatus(): void
59
    {
60
        try {
61
            if ($this->chunk->isLast()) {
62
                $this->setStatus(self::STATUS_FULFILLED);
63
            }
64
65
            if ($this->chunk->isTimeout()) {
66
                $this->setStatus(self::STATUS_TIMEOUT);
67
            }
68
        } catch (TransportExceptionInterface $e) {
69
            $this->setStatus(self::STATUS_REJECTED);
70
71
            $this->exception = $e;
72
        }
73
    }
74
75
    /**
76
     * @return string
77
     */
78
    protected function getStatus()
79
    {
80
        return $this->status;
81
    }
82
83
    /**
84
     * @param string $status
85
     *
86
     * @return $this
87
     */
88
    protected function setStatus(string $status)
89
    {
90
        $this->status = $status;
91
92
        return $this;
93
    }
94
95
    /**
96
     * @return bool
97
     */
98
    protected function isPending(): bool
99
    {
100
        return $this->getStatus() == 'PENDING';
101
    }
102
103
    /**
104
     * @return bool
105
     */
106
    protected function isFulfilled(): bool
107
    {
108
        return $this->getStatus() == 'FULFILLED';
109
    }
110
111
    /**
112
     * @return bool
113
     */
114
    protected function isTimeout(): bool
115
    {
116
        return $this->getStatus() == 'TIMEOUT';
117
    }
118
119
    /**
120
     * @return bool
121
     */
122
    protected function isRejected(): bool
123
    {
124
        return $this->getStatus() == 'REJECTED';
125
    }
126
127
    /**
128
     * @param callable $callback
129
     *
130
     * @return $this
131
     */
132
    public function then(callable $callback): self
133
    {
134
        if ($this->isFulfilled()) {
135
            $callback($this->response());
136
        }
137
138
        return $this;
139
    }
140
141
    /**
142
     * @param callable $callback
143
     *
144
     * @return $this
145
     */
146
    public function timeout(callable $callback): self
147
    {
148
        if ($this->isTimeout()) {
149
            $callback($this->response());
150
        }
151
152
        return $this;
153
    }
154
155
    /**
156
     * @param callable $callback
157
     *
158
     * @return $this
159
     */
160
    public function catch(callable $callback): self
161
    {
162
        if ($this->isRejected()) {
163
            $callback($this->exception, $this->response());
164
        }
165
166
        return $this;
167
    }
168
169
    /**
170
     * @return Response
171
     */
172
    protected function response(): Response
173
    {
174
        return Response::createFromResponse($this->response);
175
    }
176
}
177