Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
83.33% |
5 / 6 |
CRAP | |
93.10% |
27 / 29 |
RedisCoordinates | |
0.00% |
0 / 1 |
|
83.33% |
5 / 6 |
11.04 | |
93.10% |
27 / 29 |
__construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
3 / 3 |
|||
setToData | |
100.00% |
1 / 1 |
3 | |
100.00% |
7 / 7 |
|||
getRedisKey | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
initiateRedis | |
100.00% |
1 / 1 |
1 | |
100.00% |
6 / 6 |
|||
setCoordinatesFromRedis | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
waitCoordinatesFromRedis | |
0.00% |
0 / 1 |
4.13 | |
80.00% |
8 / 10 |
1 | <?php |
2 | |
3 | namespace Qmp\Laravel\ApiExternal\Services\Adserving; |
4 | |
5 | use Illuminate\Support\Facades\Redis; |
6 | |
7 | class RedisCoordinates |
8 | { |
9 | protected $data; |
10 | protected $wsCoordinates; |
11 | |
12 | public function __construct(Data $data, WSCoordinates $wsCoordinates) |
13 | { |
14 | $this->data = $data; |
15 | $this->wsCoordinates = $wsCoordinates; |
16 | } |
17 | |
18 | public function setToData() |
19 | { |
20 | $coordinates = Redis::get($this->getRedisKey($this->data->input)); |
21 | if ($coordinates === null) { // Not search already instanciate |
22 | $this->initiateRedis(); |
23 | } else if ($coordinates !== Data::WAITING_MESSAGE) { // Search resolved |
24 | $this->setCoordinatesFromRedis($coordinates); |
25 | } else { // Search already instanciate but not resolve |
26 | $this->waitCoordinatesFromRedis($coordinates); |
27 | } |
28 | } |
29 | |
30 | public function getRedisKey($key) |
31 | { |
32 | return 'adserving:coordinates_from_' . $key . ':' . $this->data->$key; |
33 | } |
34 | |
35 | protected function initiateRedis() |
36 | { |
37 | Redis::set($this->getRedisKey($this->data->input), Data::WAITING_MESSAGE); |
38 | Redis::expire($this->getRedisKey($this->data->input), 60); |
39 | $this->wsCoordinates->fromInput(); |
40 | Redis::set($this->getRedisKey($this->data->input),implode('/', [$this->data->latitude, $this->data->longitude])); |
41 | Redis::expire($this->getRedisKey($this->data->input), 60); |
42 | } |
43 | |
44 | protected function setCoordinatesFromRedis($coordinates) |
45 | { |
46 | [$this->data->latitude, $this->data->longitude] = explode('/', $coordinates); |
47 | } |
48 | |
49 | protected function waitCoordinatesFromRedis($coordinates) |
50 | { |
51 | $i = 0; |
52 | while ($coordinates === Data::WAITING_MESSAGE && $i < 50) { |
53 | usleep(300000); |
54 | $i++; |
55 | $coordinates = Redis::get($this->getRedisKey($this->data->input)); |
56 | } |
57 | |
58 | if ($coordinates !== Data::WAITING_MESSAGE) { |
59 | $this->setCoordinatesFromRedis($coordinates); |
60 | } else { |
61 | $this->data->debugData('No response from Redis !'); |
62 | abort(410, "Gone"); |
63 | } |
64 | } |
65 | } |