1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* For the full copyright and license information, please view the LICENSE file |
5
|
|
|
* that was distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
declare ( |
9
|
|
|
strict_types = 1 |
10
|
|
|
); |
11
|
|
|
|
12
|
|
|
namespace Component\Remote\BurzeDzisNet; |
13
|
|
|
|
14
|
|
|
use OutOfBoundsException; |
15
|
|
|
use LogicException; |
16
|
|
|
use IteratorAggregate; |
17
|
|
|
use ArrayIterator; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* WeatherAlert represents set of alerts |
21
|
|
|
* |
22
|
|
|
* @author Krzysztof Piasecki <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class WeatherAlert implements IteratorAggregate, WeatherAlertInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Alerts |
28
|
|
|
* |
29
|
|
|
* @var array Alerts |
30
|
|
|
*/ |
31
|
|
|
private $alerts; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Weather alerts |
35
|
|
|
* |
36
|
|
|
* @var WeatherAlert|null Weather alert |
37
|
|
|
*/ |
38
|
|
|
private $weatherAlert = null; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* WeatherAlert |
42
|
|
|
* |
43
|
|
|
* @param WeatherAlert|null $alert set of alerts |
44
|
|
|
*/ |
45
|
|
|
public function __construct(WeatherAlert $alert = null) |
46
|
|
|
{ |
47
|
|
|
$this->alerts = ($alert != null) ? $alert->toArray() : []; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get WeatherAlert containing new alert |
52
|
|
|
* |
53
|
|
|
* @param string $name alert name |
54
|
|
|
* @param Alert $alert |
55
|
|
|
* @return WeatherAlert new instance of WeatherAlert containing specified alert |
56
|
|
|
* @throws LogicException Alert exists |
57
|
|
|
*/ |
58
|
|
|
public function withAlert(string $name, Alert $alert): WeatherAlert |
59
|
|
|
{ |
60
|
|
|
if ($this->hasAlert($name) === true) { |
61
|
|
|
throw new LogicException(\sprintf('Alert %s exists', $name)); |
62
|
|
|
} |
63
|
|
|
$weatherAlert = clone $this; |
64
|
|
|
$weatherAlert->weatherAlert = $this; |
65
|
|
|
$weatherAlert->alerts = [$name => $alert]; |
66
|
|
|
return $weatherAlert; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get alert by name |
71
|
|
|
* |
72
|
|
|
* @param string $name alert name |
73
|
|
|
* @return Alert alert |
74
|
|
|
* @throws OutOfBoundException there is no such an alert |
75
|
|
|
*/ |
76
|
|
|
public function getAlert(string $name): Alert |
77
|
|
|
{ |
78
|
|
|
$currentLink = $this; |
79
|
|
View Code Duplication |
do { |
|
|
|
|
80
|
|
|
$alerts = $currentLink->alerts; |
81
|
|
|
if (isset($alerts[$name]) == true) { |
|
|
|
|
82
|
|
|
return $alerts[$name]; |
83
|
|
|
} |
84
|
|
|
$currentLink = $currentLink->weatherAlert; |
85
|
|
|
} while ($currentLink); |
86
|
|
|
throw new OutOfBoundsException(\sprintf("There is no such an alert like '%s'", $name)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Iterates over alerts |
91
|
|
|
* |
92
|
|
|
* @return ArrayIterator alert iterator |
93
|
|
|
*/ |
94
|
|
|
public function getIterator(): ArrayIterator |
95
|
|
|
{ |
96
|
|
|
return new ArrayIterator($this->toArray()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get all alerts |
101
|
|
|
* |
102
|
|
|
* @return array all alerts |
103
|
|
|
*/ |
104
|
|
|
public function toArray(): array |
105
|
|
|
{ |
106
|
|
|
$alerts = []; |
107
|
|
|
$currentLink = $this; |
108
|
|
|
do { |
109
|
|
|
foreach ($currentLink->alerts as $name => $alert) { |
110
|
|
|
$alerts[$name] = $alert; |
111
|
|
|
} |
112
|
|
|
$currentLink = $currentLink->weatherAlert; |
113
|
|
|
} while ($currentLink != null); |
114
|
|
|
return $alerts; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Check if specified alert exists |
119
|
|
|
* |
120
|
|
|
* @param string $name string alert name |
121
|
|
|
* @return bool if specified alert exists return true; otherwise false |
122
|
|
|
*/ |
123
|
|
|
public function hasAlert(string $name): bool |
124
|
|
|
{ |
125
|
|
|
$currentLink = $this; |
126
|
|
View Code Duplication |
do { |
|
|
|
|
127
|
|
|
$alerts = $currentLink->alerts; |
128
|
|
|
if (isset($alerts[$name]) == true) { |
|
|
|
|
129
|
|
|
return true; |
130
|
|
|
} |
131
|
|
|
$currentLink = $currentLink->weatherAlert; |
132
|
|
|
} while ($currentLink); |
133
|
|
|
return false; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.