1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace seregazhuk\PinterestBot\Api; |
4
|
|
|
|
5
|
|
|
use ReflectionClass; |
6
|
|
|
use seregazhuk\PinterestBot\Api\Providers\Provider; |
7
|
|
|
use seregazhuk\PinterestBot\Contracts\RequestInterface; |
8
|
|
|
use seregazhuk\PinterestBot\Contracts\ResponseInterface; |
9
|
|
|
use seregazhuk\PinterestBot\Exceptions\WrongProviderException; |
10
|
|
|
use seregazhuk\PinterestBot\Contracts\ProvidersContainerInterface; |
11
|
|
|
|
12
|
|
|
class ProvidersContainer implements ProvidersContainerInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* References to the request and response classes that travels |
16
|
|
|
* through the application |
17
|
|
|
* |
18
|
|
|
* @var RequestInterface |
19
|
|
|
*/ |
20
|
|
|
protected $request; |
21
|
|
|
/** |
22
|
|
|
* @var ResponseInterface |
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
|
|
|
private $providers = []; |
34
|
|
|
|
35
|
|
|
public function __construct(RequestInterface $request, ResponseInterface $response) |
36
|
|
|
{ |
37
|
|
|
$this->request = $request; |
38
|
|
|
$this->response = $response; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $provider |
43
|
|
|
* @return Provider |
44
|
|
|
* @throws WrongProviderException |
45
|
|
|
*/ |
46
|
|
|
public function getProvider($provider) |
47
|
|
|
{ |
48
|
|
|
$provider = strtolower($provider); |
49
|
|
|
|
50
|
|
|
// Check if an instance has already been initiated |
51
|
|
|
if ( ! isset($this->providers[$provider])) { |
52
|
|
|
$this->addProvider($provider); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $this->providers[$provider]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $provider |
60
|
|
|
* @throws WrongProviderException |
61
|
|
|
*/ |
62
|
|
|
private function addProvider($provider) |
63
|
|
|
{ |
64
|
|
|
$className = self::PROVIDERS_NAMESPACE . ucfirst($provider); |
65
|
|
|
|
66
|
|
|
if (!class_exists($className)) { |
67
|
|
|
throw new WrongProviderException("Provider $className not found."); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->providers[$provider] = $this->buildProvider($className); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Build Provider object with reflection API |
75
|
|
|
* |
76
|
|
|
* @param string $className |
77
|
|
|
* @return object |
78
|
|
|
* @throws WrongProviderException |
79
|
|
|
*/ |
80
|
|
|
private function buildProvider($className) |
81
|
|
|
{ |
82
|
|
|
$ref = new ReflectionClass($className); |
83
|
|
|
if (!$ref->isInstantiable()) { |
84
|
|
|
throw new WrongProviderException('Provider class is not instantiable.'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $ref->newInstanceArgs([$this->request, $this->response]); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return RequestInterface |
92
|
|
|
*/ |
93
|
|
|
public function getRequest() |
94
|
|
|
{ |
95
|
|
|
return $this->request; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return ResponseInterface |
100
|
|
|
*/ |
101
|
|
|
public function getResponse() |
102
|
|
|
{ |
103
|
|
|
return $this->response; |
104
|
|
|
} |
105
|
|
|
} |