Passed
Push — master ( 2ba90a...a5cf9c )
by Sergey
04:57
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 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
 *
28
 * @method $this setLocale(string $locale)
29
 * @method $this setHost(string $host)
30
 */
31
class EndpointsContainer
32
{
33
    /**
34
     * @var Request
35
     */
36
    protected $request;
37
38
    /**
39
     * Array with the cached endpoints
40
     * @var Endpoint[]
41
     */
42
    protected $endpoints = [];
43
44
    public function __construct(Request $request)
45
    {
46
        $this->request = $request;
47
    }
48
49
    /**
50
     * @param string $endpoint
51
     * @return Endpoint
52
     */
53
    public function __get($endpoint)
54
    {
55
        return $this->getEndpoint($endpoint);
56
    }
57
58
    /**
59
     * @param string $method
60
     * @param array $arguments
61
     * @return EndpointsContainer
62
     * @throws HeadHunterApiException
63
     */
64
    public function __call($method, $arguments)
65
    {
66
        if($this->isRequestSetter($method)) {
67
            return $this->callRequestSetter($method, $arguments);
68
        }
69
70
        throw new HeadHunterApiException("Method $method not found");
71
    }
72
73
    /**
74
     * @param string $endpoint
75
     * @return Endpoint
76
     * @throws WrongEndPointException
77
     */
78
    public function getEndpoint($endpoint)
79
    {
80
        if (!isset($this->endpoints[$endpoint])) {
81
            $this->addEndpoint($endpoint);
82
        }
83
84
        return $this->endpoints[$endpoint];
85
    }
86
87
    /**
88
     * @param string $endpoint
89
     * @throws WrongEndPointException
90
     */
91
    protected function addEndpoint($endpoint)
92
    {
93
        $class = __NAMESPACE__ . '\\' . ucfirst($endpoint);
94
95
        if (!$this->checkIsEndpoint($class)) {
96
            throw new WrongEndPointException("$endpoint is not a valid endpoint");
97
        }
98
99
        $this->endpoints[$endpoint] = new $class($this);
100
    }
101
102
    /**
103
     * @return Request
104
     */
105
    public function getRequest()
106
    {
107
        return $this->request;
108
    }
109
110
    /**
111
     * @param string $method
112
     * @param array $arguments
113
     * @return $this
114
     */
115
    protected function callRequestSetter($method, $arguments)
116
    {
117
        $this->request->$method(... $arguments);
118
119
        return $this;
120
    }
121
122
    /**
123
     * @param string $method
124
     * @return bool
125
     */
126
    protected function isRequestSetter($method)
127
    {
128
        return strpos($method, 'set') === 0 && method_exists($this->request, $method);
129
    }
130
131
    /**
132
     * @param string $class
133
     * @return bool
134
     */
135
    protected function checkIsEndpoint($class)
136
    {
137
        if(!class_exists($class)) return false;
138
139
        return in_array(Endpoint::class, class_parents($class));
140
    }
141
}