Passed
Pull Request — 2.x.x (#24)
by Koldo
17:38 queued 13:31
created

PromiseResponse   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 54.55%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 3
eloc 8
c 1
b 0
f 1
dl 0
loc 46
ccs 6
cts 11
cp 0.5455
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A fromGeneratorCallback() 0 7 1
A then() 0 6 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