Passed
Push — coroutines ( c50b0f )
by Koldo
12:12
created

PromiseResponse::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 2
c 1
b 0
f 1
dl 0
loc 8
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 4
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Antidot\React;
6
7
use Generator;
8
use React\Promise\PromiseInterface;
9
use RingCentral\Psr7\Response;
10
use function React\Promise\resolve;
11
12
final class PromiseResponse extends Response implements PromiseInterface
13
{
14
    private PromiseInterface $promise;
15
    protected $stream;
16
17
    /**
18
     * @param PromiseInterface $promise
19
     * @param mixed $body
20
     * @param int $status
21
     * @param array $headers
22
     */
23 10
    public function __construct(
24
        PromiseInterface $promise,
25
        $body = null,
26
        int $status = 200,
27
        array $headers = []
28
    ) {
29 10
        parent::__construct($status, $headers, $body);
30 10
        $this->promise = $promise;
31 10
    }
32
33
    /**
34
     * @param callable<Generator> $callback
35
     * @return static
36
     */
37
    public static function fromGeneratorCallback(callable $callback): self
38
    {
39
        return new self(resolve(function () use ($callback): Generator {
40
            /** @var Generator $generator */
41
            $generator = $callback();
42
43
            return $generator;
44
        }));
45
    }
46
47
    /**
48
     * @param callable|null $onFulfilled
49
     * @param callable|null $onRejected
50
     * @param callable|null $onProgress
51
     */
52 10
    final public function then(
53
        callable $onFulfilled = null,
54
        callable $onRejected = null,
55
        callable $onProgress = null
56
    ): PromiseInterface {
57 10
        return $this->promise->then($onFulfilled, $onRejected, $onProgress);
58
    }
59
}
60