StreamResult   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 35
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setCallback() 0 4 1
A result() 0 18 4
1
<?php
2
declare(strict_types=1);
3
4
namespace Ytake\KsqlClient\Result;
5
6
use GuzzleHttp\Psr7\Stream;
7
use Ytake\KsqlClient\Entity\EntityInterface;
8
use Ytake\KsqlClient\Entity\StreamedRow;
9
use Ytake\KsqlClient\Entity\StreamedRows;
10
use Ytake\KsqlClient\StreamConsumable;
11
12
/**
13
 * Class StreamResult
14
 */
15
class StreamResult extends AbstractResult
16
{
17
    /** @var StreamConsumable */
18
    protected $callback;
19
20
    /**
21
     * @param StreamConsumable $callback
22
     */
23
    public function setCallback(StreamConsumable $callback): void
24
    {
25
        $this->callback = $callback;
26
    }
27
28
    /**
29
     * @return EntityInterface|StreamedRows
30
     */
31
    public function result(): EntityInterface
32
    {
33
        $stream = $this->response->getBody();
34
        $streamed = [];
35
        if ($stream instanceof Stream) {
36
            while (!$stream->eof()) {
37
                $line = trim(\GuzzleHttp\Psr7\readline($stream));
38
                if (!empty($line)) {
39
                    $decode = \GuzzleHttp\json_decode($line, true);
40
                    $row = new StreamedRow($decode['row']);
41
                    call_user_func_array($this->callback, [$row]);
42
                    $streamed[] = $row;
43
                }
44
            }
45
        }
46
47
        return new StreamedRows($streamed);
48
    }
49
}
50