Weather::getOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace GNAHotelSolutions\Weather;
4
5
use GuzzleHttp\Client;
6
use Psr\Http\Message\StreamInterface;
7
8
class Weather
9
{
10
    protected $url = 'api.openweathermap.org/data/2.5/weather';
11
12
    protected $units;
13
14
    protected $language;
15
16
    protected $client;
17
18
    public function get(string $city): StreamInterface
19
    {
20
        return $this->query(['q' => $city]);
21
    }
22
23
    public function find($id): StreamInterface
24
    {
25
        return $this->query(['id' => $id]);
26
    }
27
28
    public function query(array $search): StreamInterface
29
    {
30
        return $this->client()->get("{$this->url}?{$this->queryParameters($search)}")->getBody();
31
    }
32
33
    protected function queryParameters(array $search): string
34
    {
35
        return http_build_query(array_merge($this->getOptions(), $search));
36
    }
37
38
    public function getOptions(): array
39
    {
40
        return [
41
            'APPID' => config('weather.key'),
42
            'units' => $this->units ?? config('weather.units'),
43
            'lang' => $this->language ?? config('weather.language'),
44
        ];
45
    }
46
47
    public function inUnits(string $units): self
48
    {
49
        $this->units = $units;
50
51
        return $this;
52
    }
53
54
    public function inLanguage(string $language): self
55
    {
56
        $this->language = $language;
57
58
        return $this;
59
    }
60
61
    protected function client(): Client
62
    {
63
        return $this->client ?? new Client();
64
    }
65
66
    public function using(Client $client): self
67
    {
68
        $this->client = $client;
69
70
        return $this;
71
    }
72
}
73