Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
66.67% |
4 / 6 |
CRAP | |
57.14% |
20 / 35 |
| Data | |
0.00% |
0 / 1 |
|
66.67% |
4 / 6 |
23.34 | |
57.14% |
20 / 35 |
| __get | |
100.00% |
1 / 1 |
2 | |
100.00% |
3 / 3 |
|||
| __set | |
100.00% |
1 / 1 |
2 | |
100.00% |
3 / 3 |
|||
| setCoordinates | |
100.00% |
1 / 1 |
2 | |
100.00% |
5 / 5 |
|||
| service | |
0.00% |
0 / 1 |
20 | |
0.00% |
0 / 12 |
|||
| getOnService | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 3 |
|||
| debugData | |
100.00% |
1 / 1 |
1 | |
100.00% |
9 / 9 |
|||
| 1 | <?php |
| 2 | |
| 3 | namespace Qmp\Laravel\ApiExternal\Services\Adserving; |
| 4 | |
| 5 | use Illuminate\Support\Facades\Log; |
| 6 | use Qmp\Laravel\MicroService\Client\Tools\Request as ClientRequest; |
| 7 | use Qmp\Laravel\MicroService\Client\Client; |
| 8 | |
| 9 | class Data |
| 10 | { |
| 11 | const SERVICE_ADSERVING_LOCATE = 'SERVICE_ADSERVING_LOCATE'; |
| 12 | const SERVICE_GEOLOC_IP = 'SERVICE_GEOLOC_IP'; |
| 13 | |
| 14 | const URL = 'URL'; |
| 15 | const IMG = 'IMG'; |
| 16 | |
| 17 | const WAITING_MESSAGE = 'wait...'; |
| 18 | |
| 19 | protected $data = [ |
| 20 | 'visualId' => null, |
| 21 | 'index' => null, |
| 22 | 'ip' => null, |
| 23 | 'zip_code' => null, |
| 24 | 'type' => null, |
| 25 | |
| 26 | 'input' => null, |
| 27 | |
| 28 | 'latitude' => null, |
| 29 | 'longitude' => null, |
| 30 | |
| 31 | ]; |
| 32 | |
| 33 | public function __get($key) |
| 34 | { |
| 35 | if (array_key_exists($key, $this->data)) { |
| 36 | return $this->data[$key]; |
| 37 | } |
| 38 | |
| 39 | return null; |
| 40 | } |
| 41 | |
| 42 | public function __set($key, $value) |
| 43 | { |
| 44 | if (array_key_exists($key, $this->data)) { |
| 45 | $this->data[$key] = $value; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | public function setCoordinates($lat, $lon = null) |
| 50 | { |
| 51 | if (is_array($lat)) { |
| 52 | [$this->latitude, $this->longitude] = $lat; |
| 53 | } else { |
| 54 | $this->latitude = $lat; |
| 55 | $this->longitude = $lon; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | public function service($type) |
| 60 | { |
| 61 | switch ($type) { |
| 62 | case self::SERVICE_ADSERVING_LOCATE: |
| 63 | return $this->getOnService('service_adserving', 'adserving/locate', [ |
| 64 | 'latitude' => (float) $this->latitude, |
| 65 | 'longitude' => (float) $this->longitude, |
| 66 | 'visual_id' => (int) $this->visualId, |
| 67 | 'index' => (int) $this->index |
| 68 | ]); |
| 69 | case self::SERVICE_GEOLOC_IP: |
| 70 | return $this->getOnService('service_geolocation', 'geoloc', [ |
| 71 | 'input' => 'ip', |
| 72 | 'value' => $this->ip, |
| 73 | 'output' => 'coordinates' |
| 74 | ]); |
| 75 | |
| 76 | default: |
| 77 | return null; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | protected function getOnService($service, $url, $query) |
| 82 | { |
| 83 | $request = ClientRequest::createObject($service, $url, [ |
| 84 | 'query' => $query |
| 85 | ]); |
| 86 | return Client::systemSend('get', $request); |
| 87 | } |
| 88 | |
| 89 | public function debugData(string $message, array $moreData = []) |
| 90 | { |
| 91 | Log::debug($message . var_export([ |
| 92 | 'url' => \Request::getRequestUri(), |
| 93 | 'input' => $this->input, |
| 94 | 'ip' => $this->ip, |
| 95 | 'zip_code' => $this->zip_code, |
| 96 | 'latitude' => $this->latitude, |
| 97 | 'longitude' => $this->longitude, |
| 98 | ] + $moreData, true)); |
| 99 | } |
| 100 | } |