Completed
Push — master ( 18b606...1191a0 )
by yuuki
04:00
created

ResultsSession::yieldResults()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Ytake\PrestoClient;
5
6
/**
7
 * Class ResultsSession
8
 */
9
class ResultsSession
10
{
11
    /** @var QueryResult[] */
12
    private $results = [];
13
14
    /** @var StatementClient */
15
    private $prestoClient;
16
17
    /** @var int */
18
    private $timeout = 500000;
19
20
    /** @var bool */
21
    private $debug = false;
22
23
    /**
24
     * @param StatementClient $prestoClient
25
     * @param int             $timeout
26
     * @param bool            $debug
27
     */
28
    public function __construct(StatementClient $prestoClient, int $timeout = 500000, bool $debug = false)
29
    {
30
        $this->prestoClient = $prestoClient;
31
        $this->timeout = $timeout;
32
        $this->debug = $debug;
33
    }
34
35
    /**
36
     * @return ResultsSession
37
     */
38
    public function execute(): ResultsSession
39
    {
40
        $this->prestoClient->execute($this->timeout, $this->debug);
41
42
        return $this;
43
    }
44
45
    /**
46
     * @return \Generator
47
     */
48
    public function yieldResults(): \Generator
49
    {
50
        while ($this->prestoClient->isValid()) {
51
            yield $this->prestoClient->current();
52
            $this->prestoClient->advance();
53
        }
54
    }
55
56
    /**
57
     * @return array
58
     */
59
    public function getResults(): array
60
    {
61
        while ($this->prestoClient->isValid()) {
62
            $this->addResults($this->prestoClient->current());
63
            $this->prestoClient->advance();
64
        }
65
66
        return $this->results;
67
    }
68
69
    /**
70
     * @param QueryResult $queryResult
71
     */
72
    private function addResults(QueryResult $queryResult)
73
    {
74
        $this->results[] = $queryResult;
75
    }
76
}
77