Passed
Push — master ( c49abf...239941 )
by Scrutinizer
02:00
created

ParserProtectedMethods::isFlagUpAsEvent()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 23
rs 9.8666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace KEINOS\MSTDN_TOOLS\Parser;
6
7
class ParserProtectedMethods extends ParserStaticMethods
8
{
9
    /** @var bool */
10
    protected $flagByteLen;
11
    /** @var bool */
12
    protected $flagEventDelete;
13
    /** @var bool */
14
    protected $flagEventUpdate;
15
    /** @var string */
16
    protected $buffer;
17
18
    public function __construct()
19
    {
20
        $this->resetFlags();
21
        $this->clearBuffer();
22
    }
23
24
    /**
25
     * Buffers chunked payloads until accomplishes the data.
26
     *
27
     * @param  string $line
28
     * @return bool|array<string,mixed>
29
     */
30
    protected function bufferPayloadUpdate(string $line)
31
    {
32
        if (self::isDataBeginPayload($line)) {
33
            $this->buffer = self::trimEOL($line);
34
        } else {
35
            $this->buffer .= self::trimEOL($line);
36
        }
37
38
        if ($this->isDataEndPayload($line)) {
39
            $json = self::extractDataFromString($this->buffer);
40
41
            if ((false === $json) or !(is_string($json))) {
42
                return false;
43
            }
44
45
            $assoc_as_array = true; // JSON_OBJECT_AS_ARRAY
46
            $array = json_decode($json, $assoc_as_array);
47
            if (null === $array) {
48
                return false;
49
            }
50
51
            $this->resetFlags();
52
            $this->clearBuffer();
53
54
            return $array;
55
        }
56
57
        return false;
58
    }
59
60
    protected function clearBuffer(): void
61
    {
62
        $this->buffer = '';
63
    }
64
65
    protected function isSkippable(string $line): bool
66
    {
67
        if (self::isBlank($line) || self::isThump($line)) {
68
            return true;
69
        }
70
71
        if (self::isByteLenOfPayload($line)) {
72
            $this->setFlagUpByteLength(true);
73
            return true;
74
        }
75
76
        return $this->isFlagUpAsEvent($line);
77
    }
78
79
    protected function isFlagUpAsEvent(string $line): bool
80
    {
81
        if (! self::isEvent($line)) {
82
            return false;
83
        }
84
85
        // Reset flags when new event begin
86
        $this->setFlagUpEventUpdate(false);
87
        $this->setFlagUpEventDelete(false);
88
        $this->clearBuffer();
89
90
        if (self::isEventDelete($line)) {
91
            $this->setFlagUpEventDelete(true);
92
            return true;
93
        }
94
95
        if (self::isEventUpdate($line)) {
96
            $this->setFlagUpEventUpdate(true);
97
            return true;
98
        }
99
100
        // Unsupported flag
101
        return false;
102
    }
103
104
    protected function isFlagUpEventDelete(): bool
105
    {
106
        return ($this->flagByteLen && $this->flagEventDelete);
107
    }
108
109
    protected function isFlagUpEventUpdate(): bool
110
    {
111
        return ($this->flagByteLen && $this->flagEventUpdate);
112
    }
113
114
    protected function resetFlags(): void
115
    {
116
        $this->setFlagUpByteLength(false);
117
        $this->setFlagUpEventDelete(false);
118
        $this->setFlagUpEventUpdate(false);
119
    }
120
121
    protected function setFlagUpByteLength(bool $flag): void
122
    {
123
        $this->flagByteLen = $flag;
124
    }
125
126
    protected function setFlagUpEventUpdate(bool $flag): void
127
    {
128
        $this->flagEventUpdate = $flag;
129
    }
130
131
    protected function setFlagUpEventDelete(bool $flag): void
132
    {
133
        $this->flagEventDelete = $flag;
134
    }
135
}
136