1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* apparat-dev |
5
|
|
|
* |
6
|
|
|
* @category Apparat |
7
|
|
|
* @package Apparat\Server |
8
|
|
|
* @subpackage Apparat\Dev\Application\Service |
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @copyright Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/*********************************************************************************** |
15
|
|
|
* The MIT License (MIT) |
16
|
|
|
* |
17
|
|
|
* Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
18
|
|
|
* |
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
21
|
|
|
* the Software without restriction, including without limitation the rights to |
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
24
|
|
|
* subject to the following conditions: |
25
|
|
|
* |
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
27
|
|
|
* copies or substantial portions of the Software. |
28
|
|
|
* |
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
35
|
|
|
***********************************************************************************/ |
36
|
|
|
|
37
|
|
|
namespace Apparat\Dev\Application\Service; |
38
|
|
|
|
39
|
|
|
use Apparat\Dev\Ports\Repository; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Repository iterator |
43
|
|
|
* |
44
|
|
|
* @package Apparat\Dev |
45
|
|
|
* @subpackage Apparat\Dev\Application |
46
|
|
|
*/ |
47
|
|
|
class RepositoryIterator implements RepositoryIteratorInterface |
48
|
|
|
{ |
49
|
|
|
/** |
50
|
|
|
* Object configuration |
51
|
|
|
* |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
protected $config; |
55
|
|
|
/** |
56
|
|
|
* Object stac |
57
|
|
|
* |
58
|
|
|
* @var array |
59
|
|
|
*/ |
60
|
|
|
protected $objectStack = []; |
61
|
|
|
/** |
62
|
|
|
* Day pointer |
63
|
|
|
* |
64
|
|
|
* @var \DateTime |
65
|
|
|
*/ |
66
|
|
|
protected $dayPointer; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Constructor |
70
|
|
|
* |
71
|
|
|
* @param array $config Object configuration |
72
|
|
|
*/ |
73
|
5 |
|
public function __construct(array $config) |
74
|
|
|
{ |
75
|
5 |
|
$this->config = $config; |
76
|
5 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Return the current object config |
80
|
|
|
* |
81
|
|
|
* @return array Object config |
82
|
|
|
*/ |
83
|
5 |
|
public function current() |
84
|
|
|
{ |
85
|
5 |
|
return $this->config[array_shift($this->objectStack)]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Forward the day pointer by a random period between 0 and 259200 seconds (= 3 days) |
90
|
|
|
*/ |
91
|
3 |
|
public function next() |
92
|
|
|
{ |
93
|
3 |
|
$this->dayPointer->add(new \DateInterval('PT'.rand(0, 259200).'S')); |
94
|
3 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Return the day pointer |
98
|
|
|
* |
99
|
|
|
* @return \DateTime Day pointer |
100
|
|
|
*/ |
101
|
5 |
|
public function key() |
102
|
|
|
{ |
103
|
5 |
|
return $this->dayPointer->format('U'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Return whether the current item is still valid |
108
|
|
|
* |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
5 |
|
public function valid() |
112
|
|
|
{ |
113
|
5 |
|
return count($this->objectStack) > 0; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Rewind the iterator and rebuild the object stack |
118
|
|
|
*/ |
119
|
5 |
|
public function rewind() |
120
|
|
|
{ |
121
|
|
|
// Recreate a random object stack |
122
|
5 |
|
$this->objectStack = []; |
123
|
5 |
|
foreach ($this->config as $objectType => $objectConfig) { |
124
|
5 |
|
$this->objectStack = array_pad( |
125
|
5 |
|
$this->objectStack, |
126
|
5 |
|
count($this->objectStack) + $objectConfig[Repository::COUNT], |
127
|
|
|
$objectType |
128
|
|
|
); |
129
|
|
|
} |
130
|
5 |
|
shuffle($this->objectStack); |
131
|
|
|
|
132
|
|
|
// Reset the day pointer |
133
|
5 |
|
$this->dayPointer = new \DateTime(); |
134
|
5 |
|
} |
135
|
|
|
} |
136
|
|
|
|