Completed
Push — main ( aea140...3fb186 )
by Mahmoud
05:41
created

WeatherFactory::setLanguage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Rawaby88\OpenWeatherMap;
4
5
use GuzzleHttp\Exception\ClientException;
6
use Rawaby88\OpenWeatherMap\Clients\WeatherClient;
7
use Rawaby88\OpenWeatherMap\Exception\NotFoundException;
8
9
/**
10
 * Class WeatherFactory.
11
 */
12
abstract class WeatherFactory
13
{
14
    /**
15
     * @var \Illuminate\Config\Repository|\Illuminate\Contracts\Foundation\Application|mixed
16
     * Description language
17
     */
18
    private $language;
19
20
    /**
21
     * @var \Illuminate\Config\Repository|\Illuminate\Contracts\Foundation\Application|mixed
22
     * Unit type standard | metric | imperial
23
     */
24
    private $unitType;
25
26
    /**
27
     * @var mixed|WeatherClient
28
     */
29
    private $client;
30
31
    /**
32
     * @var string Temperature unit Kelvin | Celsius | Fahrenheit.
33
     */
34
    public $tempUnit;
35
36
    /**
37
     * @var string Distance unit meter/sec | miles/hour.
38
     */
39
    public $distUnit;
40
41
    /**
42
     * @var string Precipitation and snow Volume unit mm.
43
     */
44
    public $volUnit = 'mm';
45
46
    /**
47
     * @var string Pressure unit hPa.
48
     */
49
    public $presUnit = 'hPa';
50
51
    /**
52
     * WeatherFactory constructor.
53
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
54
     */
55
    public function __construct()
56
    {
57
        $apiKey = config('open-weather.api_token');
58
59
        if (! is_string($apiKey) || empty($apiKey)) {
60
            throw new \InvalidArgumentException('You must provide valid API key.');
61
        }
62
63
        $this->client = app()->make(WeatherClient::class);
64
65
        $this->unitType = config('open-weather.unit');
66
        $this->language = config('open-weather.language');
67
68
        $this->setUnitsFormat();
69
    }
70
71
    /**
72
     * Set the correct values for distance unit and temperature unit.
73
     */
74
    public function setUnitsFormat(): void
75
    {
76
        switch ($this->unitType) {
77
            case 'metric':
78
                $this->tempUnit = 'Celsius';
79
                $this->distUnit = 'meter/sec';
80
                break;
81
            case 'imperial':
82
                $this->tempUnit = 'Fahrenheit';
83
                $this->distUnit = 'miles/hour';
84
                break;
85
            default:
86
                $this->tempUnit = 'Kelvin';
87
                $this->distUnit = 'meter/sec';
88
        }
89
    }
90
    
91
	
92
	/**
93
	 * @param \Illuminate\Config\Repository|\Illuminate\Contracts\Foundation\Application|mixed $unitType
94
	 */
95
	public
96
	function setUnitType( $unitType )
97
	: void
98
	{
99
		$this->unitType = $unitType;
100
	}
101
	
102
	/**
103
	 * @param \Illuminate\Config\Repository|\Illuminate\Contracts\Foundation\Application|mixed $language
104
	 */
105
	public
106
	function setLanguage( $language )
107
	: void
108
	{
109
		$this->language = $language;
110
	}
111
	
112
	/**
113
	 * @return \Illuminate\Config\Repository|\Illuminate\Contracts\Foundation\Application|mixed
114
	 */
115
	public
116
	function getLanguage()
117
	{
118
		return $this->language;
119
	}
120
	
121
	/**
122
	 * @return \Illuminate\Config\Repository|\Illuminate\Contracts\Foundation\Application|mixed
123
	 */
124
	public
125
	function getUnitType()
126
	{
127
		return $this->unitType;
128
	}
129
	
130
	/**
131
     * @param $apiCall
132
     * @param array $queryData
133
     * @throws NotFoundException
134
     * @throws \GuzzleHttp\Exception\GuzzleException
135
     * @throws \Exception
136
     */
137
    protected function queryOrFail($apiCall, array $queryData = []): object
138
    {
139
        $query = [
140
            'query' => [
141
                'lang'  => $this->language,
142
                'units' => $this->unitType,
143
                'appid' => config('open-weather.api_token'),
144
            ],
145
        ];
146
147
        foreach ($queryData as $key => $value) {
148
            $query['query'][$key] = $value;
149
        }
150
        try {
151
            $response = $this->client->get($apiCall, $query);
152
153
            return json_decode((string) $response->getBody());
154
        } catch (ClientException $exception) {
155
            $responseBodyAsString = json_decode(
156
                $exception->getResponse()
157
                          ->getBody()
158
                          ->getContents()
159
            );
160
            if ($responseBodyAsString->cod == 404 || $responseBodyAsString->cod == 400) {
161
                throw new NotFoundException($responseBodyAsString->message.' '.$exception->getMessage());
162
            } else {
163
                throw new \Exception($exception->getMessage());
164
            }
165
        }
166
    }
167
}
168