Completed
Push — master ( ff1e39...f5c078 )
by Victor Hugo
15:05
created

AbstractEndpoint   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 61
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 5 1
A buildURI() 0 3 1
A attachQueryParams() 0 4 1
A __construct() 0 3 1
1
<?php
2
3
namespace VictorAvelar\Fixer\Endpoints;
4
5
use VictorAvelar\Fixer\Contracts\ExecutorInterface;
6
use VictorAvelar\Fixer\FixerHttpClient;
7
8
abstract class AbstractEndpoint implements ExecutorInterface
9
{
10
    /**
11
     * Execution method.
12
     */
13
    const REQUEST_METHOD = "GET";
14
15
    /**
16
     * @var FixerHttpClient
17
     */
18
    protected $client;
19
20
    /**
21
     * @var string
22
     */
23
    protected $path = "";
24
25
    /**
26
     * AbstractEndpoint constructor.
27
     *
28
     * @param FixerHttpClient $client
29
     */
30
    public function __construct(FixerHttpClient $client)
31
    {
32
        $this->client = $client;
33
    }
34
35
    /**
36
     * Builds the correct URI to perform requests.
37
     *
38
     * @param array $params
39
     *
40
     * @return string
41
     */
42
    public function buildURI(array $params = []): string
43
    {
44
        return join('?', [$this->path, $this->attachQueryParams($params)]);
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function execute()
51
    {
52
        return $this->client->request(
53
            self::REQUEST_METHOD,
54
            $this->path
55
        );
56
    }
57
58
    /**
59
     * Attach query params to the request.
60
     *
61
     * @param array $params
62
     *
63
     * @return string
64
     */
65
    private function attachQueryParams(array $params = []): string
66
    {
67
        return http_build_query(array_merge($params, [
68
            FixerHttpClient::KEY_NAME => $this->client->getClientKey()
69
        ]));
70
    }
71
}
72