EndpointsContainer::__get()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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