StreamEvent   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 0
dl 0
loc 129
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B parse() 0 27 4
A parseEventData() 0 18 4
A splitEndOfStream() 0 5 2
A getData() 0 4 1
A getEventType() 0 4 1
1
<?php
2
namespace Zend\Firebase\Stream;
3
4
use InvalidArgumentException;
5
6
/**
7
 * PHP7 FIREBASE LIBRARY (http://samuelventimiglia.it/)
8
 *
9
 *
10
 * @link https://github.com/samuel20miglia/zend_Firebase
11
 * @copyright Copyright (c) 2016-now Ventimiglia Samuel - Biasin Davide
12
 * @license BSD 3-Clause License
13
 *
14
 */
15
class StreamEvent
16
{
17
18
    /**
19
     * Strng pattern END_OF_LINE
20
     *
21
     * @var string END_OF_LINE
22
     */
23
    const END_OF_LINE = "/\r\n|\n|\r/";
24
25
    /**
26
     * Raw data form stream
27
     *
28
     * @var string $data
29
     */
30
    private $data;
31
32
    /**
33
     * Type of event
34
     *
35
     * @var string $eventType
36
     */
37
    private $eventType;
38
39
    /**
40
     *
41
     * @param string $data
42
     * @param string $eventType
43
     */
44
    public function __construct(string $data = '', string $eventType = 'message')
45
    {
46
        $this->data = $data;
47
        $this->eventType = $eventType;
48
    }
49
50
    /**
51
     *
52
     * @param $raw
53
     * @return StreamEvent $event
54
     */
55
    public static function parse($raw)
56
    {
57
        $event = new StreamEvent();
58
        $lines = self::splitEndOfStream($raw);
59
60
        foreach ($lines as $line) {
61
            $matches = '';
62
            $matched = preg_match('/(?P<name>[^:]*):?( ?(?P<value>.*))?/', $line, $matches);
63
64
            if (!$matched) {
65
                throw new InvalidArgumentException(sprintf('Invalid line %s', $line));
66
            }
67
68
            $name = $matches['name'];
69
            $value = $matches['value'];
70
71
            if ($name === '') {
72
                unset($name);
73
                unset($value);
74
                // ignore comments
75
                continue;
76
            }
77
78
            $event = self::parseEventData($event, $name, $value);
79
        }
80
        return $event;
81
    }
82
83
    /**
84
     * Return Object
85
     *
86
     * @param \ZendFirebase\Stream\StreamEvent $event
87
     * @param string $name
88
     * @param string $value
89
     * @return \ZendFirebase\Stream\StreamEvent
90
     */
91
    private static function parseEventData(StreamEvent $event, string $name, string $value): StreamEvent
92
    {
93
94
95
        switch ($name) {
96
            case 'event':
97
                $event->eventType = $value;
98
                break;
99
            case 'data':
100
                $event->data = empty($event->data) ? $value : "$event->data\n$value";
101
                break;
102
103
            default:
104
                // The field is ignored.
105
                continue;
106
        }
107
        return $event;
108
    }
109
110
111
    /**
112
     * Find enf of stream
113
     *
114
     * @param mixed $raw
115
     * @return array
116
     */
117
    private static function splitEndOfStream($raw):array
118
    {
119
        $lines = preg_split(self::END_OF_LINE, $raw);
120
        return (!$lines) ?: [];
121
    }
122
123
124
    /**
125
     * All db changes
126
     *
127
     * @return string $this->data
128
     */
129
    public function getData(): string
130
    {
131
        return $this->data;
132
    }
133
134
    /**
135
     * Type of event
136
     *
137
     * @return string $this->eventType
138
     */
139
    public function getEventType(): string
140
    {
141
        return $this->eventType;
142
    }
143
}
144