Completed
Push — master ( 49f631...00219d )
by Frank
03:48
created

Client::withEndpoint()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
declare(strict_types=1);
3
4
/*
5
 * This file is part of the package neoblack/free-at-home-api.
6
 *
7
 * For the full copyright and license information, please read the
8
 * LICENSE file that was distributed with this source code.
9
 */
10
11
namespace NeoBlack\FreeAtHomeApi;
12
13
use GuzzleHttp\Exception\ClientException;
14
use GuzzleHttp\Exception\ServerException;
15
use NeoBlack\FreeAtHomeApi\Entity\Credentials;
16
use NeoBlack\FreeAtHomeApi\Entity\Route;
17
use NeoBlack\FreeAtHomeApi\Exception\InvalidEndpointException;
18
use NeoBlack\FreeAtHomeApi\Exception\MissingCredentialsException;
19
use NeoBlack\FreeAtHomeApi\Exception\MissingEndpointException;
20
use NeoBlack\FreeAtHomeApi\Service\Router;
21
22
class Client
23
{
24
    /**
25
     * @var string
26
     */
27
    protected $endpoint;
28
29
    /**
30
     * @var Credentials
31
     */
32
    protected $credentials;
33
34
    /**
35
     * @var \GuzzleHttp\Client
36
     */
37
    protected $client;
38
39 13
    public function withEndpoint(string $endpoint): self
40
    {
41 13
        if (!filter_var($endpoint, FILTER_VALIDATE_URL)) {
42 3
            throw new InvalidEndpointException('the endpoint has an invalid format', 1536354983);
43
        }
44 10
        $this->endpoint = $endpoint;
45 10
        return $this;
46
    }
47
48 1
    public function withCredentials(string $username, string $password): self
49
    {
50 1
        $this->credentials = new Credentials($username, $password);
51 1
        return $this;
52
    }
53
54 3
    public function getClient(): self
55
    {
56 3
        if ($this->endpoint === null) {
57 1
            throw new MissingEndpointException('no endpoint is set, call withEndpoint() before getClient()', 1536354535);
58
        }
59 2
        if ($this->credentials === null) {
60 1
            throw new MissingCredentialsException('no credentials is set, call withCredentials() before getClient()', 1536354568);
61
        }
62 1
        $this->client = new \GuzzleHttp\Client([
63 1
            'base_uri' => $this->endpoint,
64
        ]);
65 1
        return $this;
66
    }
67
68
    /**
69
     * @return array
70
     * @throws \GuzzleHttp\Exception\GuzzleException
71
     */
72
    public function getDevices(): array
73
    {
74
        return $this->callApi((new Router())->get(Router::DEVICES_GET));
75
    }
76
77
    /**
78
     * @param Route $route
79
     * @return array
80
     * @throws \GuzzleHttp\Exception\GuzzleException
81
     */
82
    private function callApi(Route $route): array
83
    {
84
        $options = [
85
            'headers' => [],
86
        ];
87
        try {
88
            $response = $this->client->request($route->getMethod(), $route->getPath(), $options);
89
        } catch (ClientException | ServerException $exception) {
90
            // 400-level & 500-level errors
91
            $response = $exception->getResponse();
92
        }
93
        return json_decode($response->getBody()->getContents(), true);
94
    }
95
}
96