1 | <?php |
||
11 | class ProvidersContainer |
||
12 | { |
||
13 | const PROVIDERS_NAMESPACE = 'seregazhuk\\PinterestBot\\Api\\Providers\\'; |
||
14 | |||
15 | /** |
||
16 | * References to the request that travels |
||
17 | * through the application. |
||
18 | * |
||
19 | * @var Request |
||
20 | */ |
||
21 | protected $request; |
||
22 | |||
23 | /** |
||
24 | * @var Response |
||
25 | */ |
||
26 | protected $response; |
||
27 | |||
28 | /** |
||
29 | * A array containing the cached providers. |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $providers = []; |
||
34 | |||
35 | /** |
||
36 | * @param Request $request |
||
37 | * @param Response $response |
||
38 | */ |
||
39 | public function __construct(Request $request, Response $response) |
||
44 | |||
45 | /** |
||
46 | * Gets provider object by name. If there is no such provider |
||
47 | * in providers array, it will try to create it, then save |
||
48 | * it, and then return. |
||
49 | * |
||
50 | * @param string $provider |
||
51 | * |
||
52 | * @throws WrongProvider |
||
53 | * |
||
54 | * @return Provider |
||
55 | */ |
||
56 | public function getProvider($provider) |
||
57 | { |
||
58 | $provider = strtolower($provider); |
||
59 | |||
60 | // Check if an instance has already been initiated. If not |
||
61 | // build it and then add to the providers array. |
||
62 | if (!isset($this->providers[$provider])) { |
||
63 | $this->addProvider($provider); |
||
64 | } |
||
65 | |||
66 | return $this->providers[$provider]; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Creates provider by class name, and if success saves |
||
71 | * it to providers array. Provider class must exist in PROVIDERS_NAMESPACE. |
||
72 | * |
||
73 | * @param string $provider |
||
74 | * @throws WrongProvider |
||
75 | */ |
||
76 | protected function addProvider($provider) |
||
77 | { |
||
78 | $className = $this->resolveProviderClass($provider); |
||
79 | |||
80 | $this->providers[$provider] = $this->buildProvider($className); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Build Provider object with reflection API. |
||
85 | * |
||
86 | * @param string $className |
||
87 | * @throws WrongProvider |
||
88 | * @return object |
||
89 | */ |
||
90 | protected function buildProvider($className) |
||
91 | { |
||
92 | $provider = (new ReflectionClass($className)) |
||
93 | ->newInstanceArgs([$this]); |
||
94 | |||
95 | return new ProviderWrapper($provider); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Proxies call to Request object and returns message from |
||
100 | * the error object. |
||
101 | * |
||
102 | * @return string|null |
||
103 | */ |
||
104 | public function getLastError() |
||
105 | { |
||
106 | $error = $this |
||
107 | ->response |
||
108 | ->getLastError(); |
||
109 | |||
110 | if(isset($error['message'])) return $error['message']; |
||
111 | |||
112 | if(isset($error['code'])) return $error['code']; |
||
113 | |||
114 | return null; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Simply proxies call to Response object. |
||
119 | * |
||
120 | * @return array|null |
||
121 | */ |
||
122 | public function getClientInfo() |
||
126 | |||
127 | /** |
||
128 | * Returns HttpClient object for setting user-agent string or |
||
129 | * other CURL available options. |
||
130 | * |
||
131 | * @return HttpClient |
||
132 | */ |
||
133 | public function getHttpClient() |
||
137 | |||
138 | /** |
||
139 | * @param $provider |
||
140 | * @return string |
||
141 | * @throws WrongProvider |
||
142 | */ |
||
143 | protected function resolveProviderClass($provider) |
||
153 | |||
154 | /** |
||
155 | * @return Request |
||
156 | */ |
||
157 | public function getRequest() |
||
161 | |||
162 | /** |
||
163 | * @return Response |
||
164 | */ |
||
165 | public function getResponse() |
||
169 | } |
||
170 |