1 | <?php |
||
15 | abstract class Stream extends Psr7Stream |
||
16 | { |
||
17 | /** |
||
18 | * Streaming API base URL. |
||
19 | */ |
||
20 | const BASE_URL = 'https://stream.twitter.com'; |
||
21 | |||
22 | |||
23 | /** |
||
24 | * @var Oauth |
||
25 | */ |
||
26 | protected $oauth; |
||
27 | |||
28 | /** |
||
29 | * Streaming endpoint. |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $endpoint = ''; |
||
34 | |||
35 | /** |
||
36 | * Http method to use when connecting to the streaming API. |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $httpMethod = 'GET'; |
||
41 | |||
42 | /** |
||
43 | * @var LoggerInterface |
||
44 | */ |
||
45 | protected $logger; |
||
46 | |||
47 | /** |
||
48 | * Parameters to use to filter the statuses. |
||
49 | * |
||
50 | * @var \Callisto\RequestParameter[] |
||
51 | */ |
||
52 | protected $requestParameters = []; |
||
53 | |||
54 | |||
55 | /** |
||
56 | * Stream constructor. |
||
57 | * |
||
58 | * @param Oauth $oauth |
||
59 | */ |
||
60 | 18 | public function __construct(Oauth $oauth) |
|
65 | |||
66 | /** |
||
67 | * Opens a connection to the Twitter Streaming API. |
||
68 | */ |
||
69 | 9 | public function connect() : void |
|
103 | |||
104 | /** |
||
105 | * Checks the HTTP response code recieved from the server. |
||
106 | * |
||
107 | * @param string $response |
||
108 | * @throws Exception\ConnectionException If the response code different than 200. |
||
109 | */ |
||
110 | 9 | protected function checkResponseStatusCode($response) |
|
118 | |||
119 | /** |
||
120 | * Returns the parameters to use in the request. |
||
121 | * |
||
122 | * @return array |
||
123 | */ |
||
124 | 9 | protected function getParams() : array |
|
125 | { |
||
126 | $return = [ |
||
127 | 'stall_warnings' => 'true' |
||
128 | 9 | ]; |
|
129 | |||
130 | 9 | foreach ($this->requestParameters as $filter) { |
|
131 | 1 | $return[$filter->getKey()] = $filter->getValue(); |
|
132 | } |
||
133 | |||
134 | 9 | return $return; |
|
135 | } |
||
136 | |||
137 | /** |
||
138 | * Sets the filters to use in the request. |
||
139 | * |
||
140 | * @param RequestParameter[] $requestParameters |
||
141 | * @return $this Fluent interface. |
||
142 | */ |
||
143 | 1 | public function setRequestParameters($requestParameters) |
|
144 | { |
||
145 | 1 | $this->requestParameters = $requestParameters; |
|
146 | 1 | return $this; |
|
147 | } |
||
148 | |||
149 | /** |
||
150 | * Handles the message from twitter. |
||
151 | * |
||
152 | * @param string $messageJson |
||
153 | */ |
||
154 | 1 | protected function handleMessage(string $messageJson) : void |
|
155 | { |
||
156 | 1 | $message = json_decode($messageJson); |
|
157 | 1 | $this->logger->info('Message received', [$message]); |
|
158 | 1 | } |
|
159 | |||
160 | /** |
||
161 | * Determines if the received json is a message from twitter. |
||
162 | * |
||
163 | * @param string $status |
||
164 | * @return bool |
||
165 | */ |
||
166 | 3 | private function isMessage(string $status) : bool |
|
167 | { |
||
168 | 3 | $testStr = substr($status, 0, 14); |
|
169 | 3 | if ('{"created_at":' == $testStr) { |
|
170 | 3 | return false; |
|
171 | } |
||
172 | |||
173 | 1 | return true; |
|
174 | } |
||
175 | |||
176 | /** |
||
177 | * Override the default NullLogger |
||
178 | * |
||
179 | * @param LoggerInterface $logger |
||
180 | * @return Stream $this Fluent interface. |
||
181 | */ |
||
182 | 18 | public function setLogger(LoggerInterface $logger) : Stream |
|
183 | { |
||
184 | 18 | $this->logger = $logger; |
|
185 | 18 | return $this; |
|
186 | } |
||
187 | |||
188 | /** |
||
189 | * Reads the next chunk of $chunkSize from the stream. |
||
190 | * |
||
191 | * @param int $chunkSize |
||
192 | * @return string |
||
193 | */ |
||
194 | 3 | protected function readChunk(int $chunkSize) : string |
|
198 | |||
199 | /** |
||
200 | * Reads the size of the next chunk from the stream. |
||
201 | * |
||
202 | * @return int |
||
203 | * @throws Exception\ConnectionClosedException |
||
204 | */ |
||
205 | 3 | protected function readNextChunkSize() : int |
|
219 | |||
220 | /** |
||
221 | * Connects to the Twitter API and starts reading the stream. |
||
222 | * |
||
223 | * When it recieves a new status it will be passed on to the @link self::enqueueStatus() method. |
||
224 | * |
||
225 | * @return \Generator |
||
226 | */ |
||
227 | 3 | public function readStream() : \Generator |
|
255 | |||
256 | /** |
||
257 | * Decides weather the read chunk is the end of a complete json object. |
||
258 | * |
||
259 | * @param string $chunk |
||
260 | * @param int $chunkSize |
||
261 | * @return bool |
||
262 | */ |
||
263 | 3 | protected function isJsonFinished($chunk, $chunkSize) |
|
267 | |||
268 | /** |
||
269 | * Decides weather the read chunk is empty or not. |
||
270 | * |
||
271 | * @param int $chunkSize |
||
272 | * @return bool |
||
273 | */ |
||
274 | 3 | protected function isEmptyChunk($chunkSize) |
|
278 | } |
||
279 |