Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
DeliveryMonitoringIterator
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 7
156
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 rewind
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 current
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 key
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 valid
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 next
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
20
 load
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
6
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) 2018 (original work) Open Assessment Technologies SA;
19 */
20
21namespace oat\taoProctoring\model\monitorCache\implementation;
22
23use Iterator;
24use oat\taoProctoring\model\monitorCache\DeliveryMonitoringService;
25use Zend\ServiceManager\ServiceLocatorAwareTrait;
26use Zend\ServiceManager\ServiceLocatorInterface;
27
28/**
29 * Class DeliveryMonitoringIterator
30 * @package oat\taoProctoring\model\monitorCache\implementation
31 */
32class DeliveryMonitoringIterator implements \Iterator
33{
34    use ServiceLocatorAwareTrait;
35
36    public const CACHE_SIZE = 10000;
37
38    /**
39     * Id of the current instance
40     *
41     * @var int
42     */
43    private $currentInstance = 0;
44
45    /**
46     * List of resource uris currently being iterated over
47     *
48     * @var array
49     */
50    private $instanceCache = null;
51
52    /**
53     * Indicater whenever the end of  the current cache is also the end of the current class
54     *
55     * @var boolean
56     */
57    private $endOfResource = false;
58
59    /**
60     * Whenever we already moved the pointer, used to prevent unnecessary rewinds
61     *
62     * @var boolean
63     */
64    private $unmoved = true;
65
66    /**
67     * DeliveryMonitoringIterator constructor.
68     * @param ServiceLocatorInterface $serviceLocator
69     * @throws \common_exception_Error
70     */
71    public function __construct(ServiceLocatorInterface $serviceLocator)
72    {
73        $this->setServiceLocator($serviceLocator);
74        $this->load(0);
75    }
76
77    /**
78     * (non-PHPdoc)
79     * @see Iterator::rewind()
80     */
81    public function rewind()
82    {
83        if (!$this->unmoved) {
84            $this->unmoved = true;
85        }
86    }
87
88    /**
89     * (non-PHPdoc)
90     * @see Iterator::current()
91     */
92    public function current()
93    {
94        return $this->instanceCache[$this->currentInstance];
95    }
96
97    /**
98     * (non-PHPdoc)
99     * @see Iterator::key()
100     */
101    public function key()
102    {
103        return $this->currentInstance;
104    }
105
106
107    public function valid()
108    {
109        return isset($this->instanceCache[$this->currentInstance]);
110    }
111
112    /**
113     * (non-PHPdoc)
114     * @see Iterator::next()
115     */
116    public function next()
117    {
118        $this->unmoved = false;
119        if ($this->valid()) {
120            $this->currentInstance++;
121            if (!isset($this->instanceCache[$this->currentInstance])) {
122                // try to load next block (unless we know it's empty)
123                $remainingInstances = !$this->endOfResource && $this->load($this->currentInstance);
124            }
125        }
126    }
127
128    /**
129     * @param $offset
130     * @return bool
131     */
132    protected function load($offset)
133    {
134        \common_Logger::d(__CLASS__ . '::load offset = ' . $offset);
135
136        /** @var DeliveryMonitoringService $deliveryMonitoringService */
137        $deliveryMonitoringService = $this->getServiceLocator()->get(DeliveryMonitoringService::SERVICE_ID);
138        $options = array(
139            'offset' => $offset,
140            'limit' => self::CACHE_SIZE
141        );
142
143        $executions = $deliveryMonitoringService->find([], $options);
144
145        $this->instanceCache = array();
146
147        foreach ($executions as $execution) {
148            $this->instanceCache[$offset] = $execution->get();
149            $offset++;
150        }
151
152        $this->endOfResource = count($executions) < self::CACHE_SIZE;
153
154        return count($executions) > 0;
155    }
156}