Passed
Push — master ( cceb32...6bc369 )
by Scrutinizer
01:52 queued 39s
created

ParserProtectedMethods::setFlagUpByteLength()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace KEINOS\MSTDN_TOOLS;
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 = trim($line);
34
        } else {
35
            $this->buffer .= trim($line);
36
        }
37
38
        if ($this->isDataEndPayload($line)) {
39
            $json = self::extractDataFromString($this->buffer);
40
            $assoc_as_array = true; // JSON_OBJECT_AS_ARRAY
41
            $array = json_decode($json, $assoc_as_array);
42
            if (null === $array) {
43
                return false;
44
            }
45
46
            $this->resetFlags();
47
            $this->clearBuffer();
48
49
            return $array;
50
        }
51
52
        return false;
53
    }
54
55
    protected function clearBuffer(): void
56
    {
57
        $this->buffer = '';
58
    }
59
60
    protected function isSkippable(string $line): bool
61
    {
62
        if (self::isBlank($line)) {
63
            return true;
64
        }
65
66
        if (self::isThump($line)) {
67
            return true;
68
        }
69
70
        if (self::isByteLenOfPayload($line)) {
71
            $this->setFlagUpByteLength(true);
72
            return true;
73
        }
74
75
        // Reset the flags when events begin
76
        if (self::isEvent($line)) {
77
            $this->setFlagUpEventDelete(false);
78
            $this->setFlagUpEventUpdate(false);
79
            $this->clearBuffer();
80
        }
81
82
        if (self::isEventDelete($line)) {
83
            $this->setFlagUpEventDelete(true);
84
            return true;
85
        }
86
87
        if (self::isEventUpdate($line)) {
88
            $this->setFlagUpEventUpdate(true);
89
            return true;
90
        }
91
92
        return false;
93
    }
94
95
    protected function isFlagUpEventDelete(): bool
96
    {
97
        return ($this->flagByteLen && $this->flagEventDelete);
98
    }
99
100
    protected function isFlagUpEventUpdate(): bool
101
    {
102
        return ($this->flagByteLen && $this->flagEventUpdate);
103
    }
104
105
    protected function resetFlags(): void
106
    {
107
        $this->setFlagUpByteLength(false);
108
        $this->setFlagUpEventDelete(false);
109
        $this->setFlagUpEventUpdate(false);
110
    }
111
112
    protected function setFlagUpByteLength(bool $flag): void
113
    {
114
        $this->flagByteLen = $flag;
115
    }
116
117
    protected function setFlagUpEventUpdate(bool $flag): void
118
    {
119
        $this->flagEventUpdate = $flag;
120
    }
121
122
    protected function setFlagUpEventDelete(bool $flag): void
123
    {
124
        $this->flagEventDelete = $flag;
125
    }
126
}
127