StatusStream::fail()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 9
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 6
1
<?php declare(strict_types = 1);
2
3
namespace PeeHaa\AsyncTwitter\Api;
4
5
use Amp\Deferred;
6
use Amp\Failure;
7
use Amp\Promise;
8
use Amp\Success;
9
10
class StatusStream implements Stream
11
{
12
    private $buffer = [];
13
14
    /**
15
     * @var Deferred
16
     */
17
    private $readPromisor = null;
18
19
    private $error = null;
20
    private $ended = false;
21
22
    private function resolveReadPromise($value): bool
23
    {
24
        $promisor = $this->readPromisor;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
25
        $this->readPromisor = null;
26
27
        if ($promisor === null) {
28
            return false;
29
        }
30
31
        $promisor->succeed($value);
0 ignored issues
show
Bug introduced by
The method succeed() does not seem to exist on object<Amp\Deferred>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
32
33
        return true;
34
    }
35
36
    public function update($status)
37
    {
38
        if (!$this->resolveReadPromise($status)) {
39
            $this->buffer[] = $status;
40
        }
41
    }
42
43
    public function end()
44
    {
45
        $this->ended = true;
46
47
        $this->resolveReadPromise(null);
48
    }
49
50
    public function fail(\Throwable $error)
51
    {
52
        $this->error = $error;
53
54
        $promisor = $this->readPromisor;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
55
        $this->readPromisor = null;
56
57
        if ($promisor !== null) {
58
            $promisor->fail($error);
59
        }
60
    }
61
62
    public function read(): Promise
63
    {
64
        if ($this->error !== null) {
65
            return new Failure($this->error);
66
        }
67
68
        if ($this->ended) {
69
            return new Success(null);
70
        }
71
72
        if (count($this->buffer) > 0) {
73
            return new Success(\array_shift($this->buffer));
74
        }
75
76
        if ($this->readPromisor === null) {
77
            $this->readPromisor = new Deferred;
78
        }
79
80
        return $this->readPromisor->promise();
81
    }
82
83
    public function close()
84
    {
85
        $this->end(); // todo
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
86
    }
87
}
88