1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace seregazhuk\PinterestBot\Api; |
4
|
|
|
|
5
|
|
|
use ReflectionClass; |
6
|
|
|
use seregazhuk\PinterestBot\Api\Contracts\HttpClient; |
7
|
|
|
use seregazhuk\PinterestBot\Api\Providers\Provider; |
8
|
|
|
use seregazhuk\PinterestBot\Api\Providers\ProviderWrapper; |
9
|
|
|
use seregazhuk\PinterestBot\Exceptions\WrongProviderException; |
10
|
|
|
|
11
|
|
|
class ProvidersContainer |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* References to the request that travels |
15
|
|
|
* through the application. |
16
|
|
|
* |
17
|
|
|
* @var Request |
18
|
|
|
*/ |
19
|
|
|
protected $request; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Response |
23
|
|
|
*/ |
24
|
|
|
protected $response; |
25
|
|
|
|
26
|
|
|
const PROVIDERS_NAMESPACE = 'seregazhuk\\PinterestBot\\Api\\Providers\\'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* A array containing the cached providers. |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $providers = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param Request $request |
37
|
|
|
*/ |
38
|
|
|
public function __construct(Request $request) |
39
|
|
|
{ |
40
|
|
|
$this->request = $request; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Gets provider object by name. If there is no such provider |
45
|
|
|
* in providers array, it will try to create it, then save |
46
|
|
|
* it, and then return. |
47
|
|
|
* |
48
|
|
|
* @param string $provider |
49
|
|
|
* |
50
|
|
|
* @throws WrongProviderException |
51
|
|
|
* |
52
|
|
|
* @return Provider |
53
|
|
|
*/ |
54
|
|
|
public function getProvider($provider) |
55
|
|
|
{ |
56
|
|
|
$provider = strtolower($provider); |
57
|
|
|
|
58
|
|
|
// Check if an instance has already been initiated |
59
|
|
|
if (!isset($this->providers[$provider])) { |
60
|
|
|
$this->addProvider($provider); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $this->providers[$provider]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Creates provider by class name, and if success saves |
68
|
|
|
* it to providers array. Provider class must be in PROVIDERS_NAMESPACE. |
69
|
|
|
* |
70
|
|
|
* @param string $provider |
71
|
|
|
* |
72
|
|
|
* @throws WrongProviderException |
73
|
|
|
*/ |
74
|
|
|
protected function addProvider($provider) |
75
|
|
|
{ |
76
|
|
|
$className = self::PROVIDERS_NAMESPACE.ucfirst($provider); |
77
|
|
|
|
78
|
|
|
if (!class_exists($className)) { |
79
|
|
|
throw new WrongProviderException("Provider $className not found."); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this->providers[$provider] = $this->buildProvider($className); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Build Provider object with reflection API. |
87
|
|
|
* |
88
|
|
|
* @param string $className |
89
|
|
|
* |
90
|
|
|
* @throws WrongProviderException |
91
|
|
|
* |
92
|
|
|
* @return object |
93
|
|
|
*/ |
94
|
|
|
protected function buildProvider($className) |
95
|
|
|
{ |
96
|
|
|
$provider = (new ReflectionClass($className)) |
97
|
|
|
->newInstanceArgs([$this->request]); |
98
|
|
|
|
99
|
|
|
return new ProviderWrapper($provider); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Proxy method to Request object. |
104
|
|
|
* |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
|
|
public function getLastError() |
108
|
|
|
{ |
109
|
|
|
return $this |
110
|
|
|
->request |
111
|
|
|
->getLastError(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Returns HttpClient object for setting user-agent string or |
116
|
|
|
* other CURL available options. |
117
|
|
|
* |
118
|
|
|
* @return HttpClient |
119
|
|
|
*/ |
120
|
|
|
public function getHttpClient() |
121
|
|
|
{ |
122
|
|
|
return $this->request->getHttpClient(); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|