1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* |
5
|
|
|
* PHP version 5.5 |
6
|
|
|
* |
7
|
|
|
* @package Forecast |
8
|
|
|
* @author Sergey V.Kuzin <[email protected]> |
9
|
|
|
* @license MIT |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Forecast; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
use Cache\Taggable\TaggablePSR6PoolAdapter; |
16
|
|
|
use Fig\Cache\Memory\MemoryPool; |
17
|
|
|
use Forecast\Helper\Point; |
18
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
19
|
|
|
use Psr\Cache\NullCacheItemPool; |
20
|
|
|
use Psr\Log\LoggerInterface; |
21
|
|
|
use Psr\Log\NullLogger; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class WeatherAbstract |
25
|
|
|
* |
26
|
|
|
* PHP version 5.5 |
27
|
|
|
* |
28
|
|
|
* @package Forecast |
29
|
|
|
* @author Sergey V.Kuzin <[email protected]> |
30
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
31
|
|
|
* |
32
|
|
|
*/ |
33
|
|
|
abstract class WeatherAbstract |
34
|
|
|
{ |
35
|
|
|
/** @var \DateTime */ |
36
|
|
|
protected $expiration = null; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @internal |
40
|
|
|
* @var TaggablePSR6PoolAdapter |
41
|
|
|
*/ |
42
|
|
|
protected $cache = null; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @internal |
46
|
|
|
* @var LoggerInterface |
47
|
|
|
*/ |
48
|
|
|
protected $logger = null; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @internal |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
protected $lang = 'en'; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @internal |
58
|
|
|
* @var string |
59
|
|
|
*/ |
60
|
|
|
protected $units = 'si'; |
61
|
|
|
|
62
|
|
|
/** Нет осадков */ |
63
|
|
|
const PRECIP_TYPE_NONE = 0; |
64
|
|
|
/** Дождь */ |
65
|
|
|
const PRECIP_TYPE_RAIN = 1; |
66
|
|
|
/** Снег */ |
67
|
|
|
const PRECIP_TYPE_SNOW = 2; |
68
|
|
|
/** Снег с дождём */ |
69
|
|
|
const PRECIP_TYPE_SLEET = 3; |
70
|
|
|
/** Град */ |
71
|
|
|
const PRECIP_TYPE_HAIL = 4; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* |
75
|
|
|
* |
76
|
|
|
* @param CacheItemPoolInterface|null $cache Экземпляра класса кэширования по стандарту PSR-6 |
77
|
|
|
* @param LoggerInterface|null $logger Экземляр класса логера сандарта PSR-3 |
78
|
|
|
* |
79
|
|
|
* @api |
80
|
|
|
*/ |
81
|
|
|
public function __construct(CacheItemPoolInterface $cache = null, LoggerInterface $logger = null) |
82
|
|
|
{ |
83
|
|
|
$this->cache = TaggablePSR6PoolAdapter::makeTaggable($cache ?: new MemoryPool()); |
84
|
|
|
$this->logger = $logger ?: new NullLogger(); |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Возвращает код языка |
91
|
|
|
* |
92
|
|
|
* @api |
93
|
|
|
* |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
public function getLang(): string |
97
|
|
|
{ |
98
|
|
|
return $this->lang; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @api |
103
|
|
|
* @param string $lang |
104
|
|
|
*/ |
105
|
|
|
public function setLang(string $lang): self |
106
|
|
|
{ |
107
|
|
|
$this->lang = $lang; |
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @api |
113
|
|
|
* |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
|
|
public function getUnits(): string |
117
|
|
|
{ |
118
|
|
|
return $this->units; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Возвращает объект с описанием текущей погоды |
123
|
|
|
* |
124
|
|
|
* @param Point $point Класс с коорденатами места для которой запрашивается погода |
125
|
|
|
* |
126
|
|
|
* @api |
127
|
|
|
* @return \Forecast\Current|null Объект с текущей погодой |
128
|
|
|
*/ |
129
|
|
View Code Duplication |
public function getCurrent(Point $point): Current |
|
|
|
|
130
|
|
|
{ |
131
|
|
|
$item = $this->cache->getItem($this->getCacheKeyCurrent($point)); |
132
|
|
|
if (!$item->isHit()) { |
133
|
|
|
$current = $this->doFetchCurrent($point); |
134
|
|
|
$item |
135
|
|
|
->set($current) |
136
|
|
|
->expiresAt($this->getCacheExpirationCurrent()) |
137
|
|
|
->setTags(['weather']); |
138
|
|
|
|
139
|
|
|
$this->cache->save($item); |
140
|
|
|
} |
141
|
|
|
return $item->get(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @api |
146
|
|
|
* |
147
|
|
|
* @param Point $point |
148
|
|
|
* @param bool $forse |
149
|
|
|
* @return Hourly |
150
|
|
|
*/ |
151
|
|
View Code Duplication |
public function getHourly(Point $point, bool $forse = false): Hourly |
|
|
|
|
152
|
|
|
{ |
153
|
|
|
$item = $this->cache->getItem($this->getCacheKeyHourly($point)); |
154
|
|
|
if ($forse || !$item->isHit()) { |
155
|
|
|
$item |
156
|
|
|
->set($this->doFetchHourly($point)) |
157
|
|
|
->expiresAt($this->getCacheExpirationHourly()) |
158
|
|
|
->setTags(['weather']); |
159
|
|
|
$this->cache->save($item); |
160
|
|
|
} |
161
|
|
|
return $item->get(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* |
166
|
|
|
*/ |
167
|
|
|
public function flushCache() |
168
|
|
|
{ |
169
|
|
|
$this->cache->clearTags(['weather']); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @api |
175
|
|
|
* |
176
|
|
|
* @param string $units |
177
|
|
|
* @return $this |
178
|
|
|
*/ |
179
|
|
|
public function setUnits(string $units): self |
180
|
|
|
{ |
181
|
|
|
$this->units = $units; |
182
|
|
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param Point $point |
187
|
|
|
* |
188
|
|
|
* @internal |
189
|
|
|
* @return Current |
190
|
|
|
*/ |
191
|
|
|
abstract protected function doFetchCurrent(Point $point); |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @param Point $point |
195
|
|
|
* @return Hourly |
196
|
|
|
*/ |
197
|
|
|
abstract protected function doFetchHourly(Point $point); |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param Point $point |
201
|
|
|
* |
202
|
|
|
* @internal |
203
|
|
|
* @return string |
204
|
|
|
*/ |
205
|
|
|
abstract protected function getCacheKeyCurrent(Point $point); |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param Point $point |
209
|
|
|
* |
210
|
|
|
* @internal |
211
|
|
|
* @return string |
212
|
|
|
*/ |
213
|
|
|
abstract protected function getCacheKeyHourly(Point $point); |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* |
217
|
|
|
* @internal |
218
|
|
|
* @return \DateTime |
219
|
|
|
*/ |
220
|
|
|
abstract protected function getCacheExpirationCurrent(); |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* |
224
|
|
|
* @internal |
225
|
|
|
* @return \DateTime |
226
|
|
|
*/ |
227
|
|
|
abstract protected function getCacheExpirationHourly(); |
228
|
|
|
|
229
|
|
|
|
230
|
|
|
/* abstract public function getDaily(); |
|
|
|
|
231
|
|
|
|
232
|
|
|
abstract public function getHistory();*/ |
233
|
|
|
|
234
|
|
|
public function calcDewPoint(float $h) |
|
|
|
|
235
|
|
|
{ |
236
|
|
|
|
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
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.