Completed
Push — master ( c44a53...3a33b3 )
by Sergey
02:56
created

EndpointsContainer::getRateInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace seregazhuk\Favro\Api\Endpoints;
4
5
use ReflectionClass;
6
use seregazhuk\Favro\Contracts\HttpClient;
7
use seregazhuk\Favro\Exceptions\WrongEndpoint;
8
9
class EndpointsContainer
10
{
11
    const ENDPOINTS_NAMESPACE = 'seregazhuk\\Favro\\Api\\Endpoints\\';
12
13
    /*
14
    * @var HttpInterface
15
    */
16
    protected $httpClient;
17
18
    /*
19
     * @var array
20
     */
21
    protected $endpoints = [];
22
23
    /**
24
     * @param HttpClient $httpClient
25
     */
26
    public function __construct(HttpClient $httpClient)
27
    {
28
        $this->httpClient = $httpClient;
0 ignored issues
show
Documentation Bug introduced by
It seems like $httpClient of type object<seregazhuk\Favro\Contracts\HttpClient> is incompatible with the declared type object<seregazhuk\Favro\...ndpoints\HttpInterface> of property $httpClient.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
29
    }
30
31
    /**
32
     * @param string $endpoint
33
     * @return Endpoint
34
     */
35
    public function resolve($endpoint)
36
    {
37
        $endpoint = strtolower($endpoint);
38
39
        // Check if an instance has already been initiated
40
        if (!isset($this->endpoints[$endpoint])) {
41
            $this->addProvider($endpoint);
42
        }
43
44
        return $this->endpoints[$endpoint];
45
    }
46
47
    /**
48
     * @param $endpoint
49
     * @throws WrongEndpoint
50
     */
51
    protected function addProvider($endpoint)
52
    {
53
        $className = self::ENDPOINTS_NAMESPACE . ucfirst($endpoint);
54
55
        if (!class_exists($className)) {
56
            throw new WrongEndpoint("Endpoint $className not found.");
57
        }
58
59
        $this->endpoints[$endpoint] = $this->buildEndpoint($className);
60
    }
61
62
    /**
63
     * @param string $className
64
     * @return Endpoint|object
65
     */
66
    protected function buildEndpoint($className)
67
    {
68
        return (new ReflectionClass($className))->newInstanceArgs([$this->httpClient]);
69
    }
70
71
    /**
72
     * @return array
73
     */
74
    public function getRateInfo()
75
    {
76
        $responseHeaders = $this->httpClient->getResponseHeaders();
77
78
        return [
79
            'reset'     => $responseHeaders['X-RateLimit-Reset'][0],
80
            'limit'     => $responseHeaders['X-RateLimit-Limit'][0],
81
            'remaining' => $responseHeaders['X-RateLimit-Remaining'][0],
82
        ];
83
    }
84
}