Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
92.86% |
26 / 28 |
|
66.67% |
4 / 6 |
CRAP | |
0.00% |
0 / 1 |
| RdsLogIterator | |
92.86% |
26 / 28 |
|
66.67% |
4 / 6 |
12.05 | |
0.00% |
0 / 1 |
| __construct | |
90.00% |
9 / 10 |
|
0.00% |
0 / 1 |
3.01 | |||
| current | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| next | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
3 | |||
| key | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| valid | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
| rewind | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2017 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | namespace oat\taoEventLog\model; |
| 24 | |
| 25 | use Doctrine\DBAL\Query\QueryBuilder; |
| 26 | |
| 27 | /** |
| 28 | * Class RdsLogIterator |
| 29 | * @package oat\taoEventLog\model\requestLog\rds |
| 30 | * @author Aleh Hutnikau, <hutnikau@1pt.com> |
| 31 | */ |
| 32 | class RdsLogIterator implements \Iterator |
| 33 | { |
| 34 | /** @var QueryBuilder */ |
| 35 | protected $queryBuilder; |
| 36 | |
| 37 | /** @var \common_persistence_SqlPersistence */ |
| 38 | protected $persistence; |
| 39 | |
| 40 | /** @var array */ |
| 41 | protected $current; |
| 42 | |
| 43 | /** @var int */ |
| 44 | protected $initialLimit; |
| 45 | |
| 46 | /** @var int */ |
| 47 | protected $firstResult; |
| 48 | |
| 49 | /** @var int */ |
| 50 | protected $currentKey; |
| 51 | |
| 52 | /** |
| 53 | * RdsRequestLogIterator constructor. |
| 54 | * @param \common_persistence_SqlPersistence $persistence |
| 55 | * @param QueryBuilder $queryBuilder |
| 56 | */ |
| 57 | public function __construct(\common_persistence_SqlPersistence $persistence, QueryBuilder $queryBuilder) |
| 58 | { |
| 59 | $this->queryBuilder = $queryBuilder; |
| 60 | $this->persistence = $persistence; |
| 61 | $this->initialLimit = $queryBuilder->getMaxResults(); |
| 62 | if ($this->initialLimit === null) { |
| 63 | $this->initialLimit = PHP_INT_MAX; |
| 64 | } |
| 65 | $this->firstResult = $queryBuilder->getFirstResult(); |
| 66 | if ($this->firstResult === null) { |
| 67 | $this->firstResult = 0; |
| 68 | } |
| 69 | $this->queryBuilder->setMaxResults(1); |
| 70 | $this->rewind(); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * |
| 75 | */ |
| 76 | public function current() |
| 77 | { |
| 78 | return $this->current; |
| 79 | } |
| 80 | |
| 81 | public function next() |
| 82 | { |
| 83 | if ($this->valid()) { |
| 84 | $this->queryBuilder->setFirstResult($this->currentKey); |
| 85 | $sql = $this->queryBuilder->getSQL(); |
| 86 | $params = $this->queryBuilder->getParameters(); |
| 87 | $stmt = $this->persistence->query($sql, $params); |
| 88 | $data = $stmt->fetch(\PDO::FETCH_ASSOC); |
| 89 | if (empty($data)) { |
| 90 | $this->current = null; |
| 91 | } else { |
| 92 | $this->current = $data; |
| 93 | } |
| 94 | $this->currentKey++; |
| 95 | } else { |
| 96 | $this->current = null; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | public function key() |
| 101 | { |
| 102 | return $this->currentKey - 1; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * @return bool |
| 107 | */ |
| 108 | public function valid() |
| 109 | { |
| 110 | if ($this->currentKey === $this->firstResult) { |
| 111 | //initial state |
| 112 | return true; |
| 113 | } |
| 114 | |
| 115 | return $this->current !== null && ($this->currentKey + $this->firstResult) < $this->initialLimit; |
| 116 | } |
| 117 | |
| 118 | public function rewind() |
| 119 | { |
| 120 | $this->currentKey = $this->firstResult; |
| 121 | $this->next(); |
| 122 | } |
| 123 | } |