Completed
Push — master ( fabb3f...b6bba9 )
by Sergey
04:46
created

EndpointsContainer::getEndpoint()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace seregazhuk\HeadHunterApi\EndPoints;
4
5
use ReflectionClass;
6
use seregazhuk\HeadHunterApi\Request;
7
use seregazhuk\HeadHunterApi\Exceptions\HeadHunterApiException;
8
use seregazhuk\HeadHunterApi\Exceptions\WrongEndPointException;
9
10
/**
11
 * Class EndpointsContainer
12
 * @package seregazhuk\HeadHunterApi\EndPoints
13
 *
14
 * @property Vacancies $vacancies
15
 * @property Employers $employers
16
 * @property Regions $regions
17
 * @property Specializations $specializations
18
 * @property Industries $industries
19
 * @property Me $me
20
 * @property Resumes $resumes
21
 * @property Artifacts $artifacts
22
 * @property Negotiations $negotiations
23
 * @property SavedSearches $savedSearches
24
 * @property Comments $comments
25
 * @property Manager $manager
26
 * @property Dictionaries $dictionaries
27
 * @property Suggests $suggests
28
 *
29
 * @method $this setLocale(string $locale)
30
 * @method $this setHost(string $host)
31
 */
32
class EndpointsContainer
33
{
34
    /**
35
     * @var Request
36
     */
37
    protected $request;
38
39
    /**
40
     * Array with the cached endpoints
41
     * @var Endpoint[]
42
     */
43
    protected $endpoints = [];
44
45
    public function __construct(Request $request)
46
    {
47
        $this->request = $request;
48
    }
49
50
    /**
51
     * @param string $endpoint
52
     * @return Endpoint
53
     */
54
    public function __get($endpoint)
55
    {
56
        return $this->getEndpoint($endpoint);
57
    }
58
59
    /**
60
     * @param string $method
61
     * @param array $arguments
62
     * @return EndpointsContainer
63
     * @throws HeadHunterApiException
64
     */
65
    public function __call($method, $arguments)
66
    {
67
        if($this->isRequestSetter($method)) {
68
            return $this->callRequestSetter($method, $arguments);
69
        }
70
71
        throw new HeadHunterApiException("Method $method not found");
72
    }
73
74
    /**
75
     * @param string $endpoint
76
     * @return Endpoint
77
     * @throws WrongEndPointException
78
     */
79
    public function getEndpoint($endpoint)
80
    {
81
        if (!isset($this->endpoints[$endpoint])) {
82
            $this->addEndpoint($endpoint);
83
        }
84
85
        return $this->endpoints[$endpoint];
86
    }
87
88
    /**
89
     * @param string $endpoint
90
     * @throws WrongEndPointException
91
     */
92
    protected function addEndpoint($endpoint)
93
    {
94
        $class = __NAMESPACE__ . '\\' . ucfirst($endpoint);
95
96
        $isEndpoint = is_subclass_of($class, Endpoint::class);
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \seregazhuk\HeadHunterAp...dPoints\Endpoint::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
97
98
        if (!class_exists($class) && !$isEndpoint) {
99
            throw new WrongEndPointException("$endpoint is not a valid endpoint");
100
        }
101
102
        $this->endpoints[$endpoint] = new $class($this);
103
    }
104
105
    /**
106
     * @return Request
107
     */
108
    public function getRequest()
109
    {
110
        return $this->request;
111
    }
112
113
    /**
114
     * @param string $method
115
     * @param array $arguments
116
     * @return $this
117
     */
118
    protected function callRequestSetter($method, $arguments)
119
    {
120
        $this->request->$method(... $arguments);
121
122
        return $this;
123
    }
124
125
    /**
126
     * @param $method
127
     * @return bool
128
     */
129
    protected function isRequestSetter($method)
130
    {
131
        return strpos($method, 'set') === 0 && method_exists($this->request, $method);
132
    }
133
}