ResultsSession::addResults()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16
 */
17
18
namespace Ytake\PrestoClient;
19
20
/**
21
 * Class ResultsSession
22
 *
23
 * @author Yuuki Takezawa <[email protected]>
24
 */
25
class ResultsSession
26
{
27
    /** @var QueryResult[] */
28
    private $results = [];
29
30
    /** @var StatementClient */
31
    private $prestoClient;
32
33
    /** @var int */
34
    private $timeout = 500000;
35
36
    /** @var bool */
37
    private $debug = false;
38
39
    /**
40
     * @param StatementClient $prestoClient
41
     * @param int             $timeout
42
     * @param bool            $debug
43
     */
44
    public function __construct(StatementClient $prestoClient, int $timeout = 500000, bool $debug = false)
45
    {
46
        $this->prestoClient = $prestoClient;
47
        $this->timeout = $timeout;
48
        $this->debug = $debug;
49
    }
50
51
    /**
52
     * @return ResultsSession
53
     */
54
    public function execute(): ResultsSession
55
    {
56
        $this->prestoClient->execute($this->timeout, $this->debug);
57
58
        return $this;
59
    }
60
61
    /**
62
     * @return \Generator
63
     */
64
    public function yieldResults(): \Generator
65
    {
66
        while ($this->prestoClient->isValid()) {
67
            yield $this->prestoClient->current();
68
            $this->prestoClient->advance();
69
        }
70
    }
71
72
    /**
73
     * @return array
74
     */
75
    public function getResults(): array
76
    {
77
        while ($this->prestoClient->isValid()) {
78
            $this->addResults($this->prestoClient->current());
79
            $this->prestoClient->advance();
80
        }
81
82
        return $this->results;
83
    }
84
85
    /**
86
     * @param QueryResult $queryResult
87
     */
88
    private function addResults(QueryResult $queryResult)
89
    {
90
        $this->results[] = $queryResult;
91
    }
92
}
93