Completed
Push — master ( df586c...625f62 )
by Sergey
04:49 queued 02:14
created

EndpointsContainer::__call()   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 2
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
        if (!class_exists($class)) {
97
            throw new WrongEndPointException;
98
        }
99
100
        $this->endpoints[$endpoint] = $this->createEndpoint($class);
101
    }
102
103
    /**
104
     * @param string $class
105
     * @return Endpoint|object
106
     * @throws WrongEndPointException
107
     */
108
    protected function createEndpoint($class)
109
    {
110
        $reflector = new ReflectionClass($class);
111
112
        if(!$reflector->isInstantiable()) {
113
            throw new WrongEndPointException("Endpoint $class is not instantiable.");
114
        }
115
116
        return $reflector->newInstanceArgs([$this]);
117
    }
118
119
    /**
120
     * @return Request
121
     */
122
    public function getRequest()
123
    {
124
        return $this->request;
125
    }
126
127
    /**
128
     * @param string $method
129
     * @param array $arguments
130
     * @return $this
131
     */
132
    protected function callRequestSetter($method, $arguments)
133
    {
134
        $this->request->$method(... $arguments);
135
136
        return $this;
137
    }
138
139
    /**
140
     * @param $method
141
     * @return bool
142
     */
143
    protected function isRequestSetter($method)
144
    {
145
        return strpos($method, 'set') === 0 && method_exists($this->request, $method);
146
    }
147
}