CWByCoordinates::__construct()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 5
eloc 9
c 2
b 1
f 0
nc 3
nop 2
dl 0
loc 15
rs 9.6111
1
<?php
2
3
namespace Rawaby88\OpenWeatherMap\Services;
4
5
use LogicException;
6
use Rawaby88\OpenWeatherMap\Interfaces\CWSingleResultInterface;
7
use Rawaby88\OpenWeatherMap\Traits\CWSingleResultTrait;
8
use Rawaby88\OpenWeatherMap\WeatherFactory;
9
10
/**
11
 * Class CWByCoordinates.
12
 */
13
class CWByCoordinates extends WeatherFactory implements CWSingleResultInterface
14
{
15
    use CWSingleResultTrait;
16
17
    /**
18
     * @var float The latitude coordinate
19
     */
20
    protected $lat;
21
    /**
22
     * @var float The longitude coordinate
23
     */
24
    protected $lon;
25
26
    /**
27
     * CWByCoordinates constructor.
28
     *
29
     * You can call by latitude and longitude coordinates.
30
     *
31
     * @param float $lat The latitude coordinate.
32
     * @param float $lon The longitude coordinate.
33
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
34
     */
35
    public function __construct(float $lat, float $lon)
36
    {
37
        if ($lat < -90 || $lat > 90) {
38
            throw new LogicException('Wrong latitude given');
39
        }
40
41
        if ($lon < -180 || $lon > 180) {
42
            throw new LogicException('Wrong longitude given');
43
        }
44
45
        parent::__construct();
46
        $this->apiCall = 'weather';
47
        $this->lat = $lat;
48
        $this->lon = $lon;
49
        $this->params = $this->paramsToArray();
50
    }
51
52
    /**
53
     * Generate query parameters for api call.
54
     * @return array
55
     */
56
    private function paramsToArray(): array
57
    {
58
        return [
59
            'lat' => $this->lat,
60
            'lon' => $this->lon,
61
        ];
62
    }
63
}
64