Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
100.00% |
1 / 1 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
21 / 21 |
| AbstractService | |
100.00% |
1 / 1 |
|
100.00% |
6 / 6 |
8 | |
100.00% |
21 / 21 |
| query | n/a |
0 / 0 |
0 | n/a |
0 / 0 |
|||||
| __construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
5 / 5 |
|||
| boot | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| input | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| value | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| output | |
100.00% |
1 / 1 |
3 | |
100.00% |
6 / 6 |
|||
| hydrate | |
100.00% |
1 / 1 |
1 | |
100.00% |
5 / 5 |
|||
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Qmp\Laravel\Geolocation\Services\GeolocApiServices; |
| 5 | |
| 6 | use Qmp\Laravel\Geolocation\Models\Location; |
| 7 | use Illuminate\Support\Collection; |
| 8 | |
| 9 | abstract class AbstractService implements ServiceInterface |
| 10 | { |
| 11 | protected $credentials; |
| 12 | protected $input; |
| 13 | protected $value; |
| 14 | |
| 15 | abstract protected function query(); |
| 16 | |
| 17 | /** |
| 18 | * AbstractService constructor. |
| 19 | * @param $input |
| 20 | * @param $value |
| 21 | * @param Collection $config |
| 22 | */ |
| 23 | public function __construct($input, $value, Collection $config) |
| 24 | { |
| 25 | $this->input($input); |
| 26 | $this->value($value); |
| 27 | |
| 28 | $this->credentials = collect($config->get('credentials')); |
| 29 | |
| 30 | $this->boot(); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Additional actions to create service's instance |
| 35 | * |
| 36 | */ |
| 37 | protected function boot() |
| 38 | { |
| 39 | // |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Set input attribute |
| 44 | * |
| 45 | * @param $value |
| 46 | */ |
| 47 | public function input($value) |
| 48 | { |
| 49 | $this->input = $value; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Set value attribute |
| 54 | * |
| 55 | * @param $value |
| 56 | */ |
| 57 | public function value($value) |
| 58 | { |
| 59 | $this->value = $value; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Return the geolocation from the request |
| 64 | * |
| 65 | * @param array|null $value |
| 66 | * @return array|Collection |
| 67 | */ |
| 68 | public function output(array $value = null) |
| 69 | { |
| 70 | $model = Location::where($this->input, $this->value)->first(); |
| 71 | |
| 72 | if (is_null($model)) { |
| 73 | $model = $this->hydrate($this->query()); |
| 74 | } |
| 75 | |
| 76 | if ($value) { |
| 77 | return $model->only($value); |
| 78 | } |
| 79 | |
| 80 | return $model->toArray(); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Create model Location from geolocation service datas |
| 85 | * |
| 86 | * @param array $attributes |
| 87 | * @return Collection |
| 88 | * @throws \Exception |
| 89 | */ |
| 90 | protected function hydrate(array $attributes = []) |
| 91 | { |
| 92 | $model = Location::create($attributes); |
| 93 | |
| 94 | $model->push('history', [ |
| 95 | 'datetime' => (new \DateTime())->format('Y-m-d H:i:s'), |
| 96 | 'service' => get_called_class() |
| 97 | ]); |
| 98 | |
| 99 | return $model; |
| 100 | } |
| 101 | } |