1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace seregazhuk\PinterestBot\Api; |
4
|
|
|
|
5
|
|
|
use ReflectionClass; |
6
|
|
|
use seregazhuk\PinterestBot\Api\Providers\Provider; |
7
|
|
|
use seregazhuk\PinterestBot\Contracts\ProvidersContainerInterface; |
8
|
|
|
use seregazhuk\PinterestBot\Contracts\RequestInterface; |
9
|
|
|
use seregazhuk\PinterestBot\Contracts\ResponseInterface; |
10
|
|
|
use seregazhuk\PinterestBot\Exceptions\WrongProviderException; |
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
|
|
|
// Check if an instance has already been initiated |
49
|
|
|
if ( ! isset($this->providers[$provider])) { |
50
|
|
|
$this->addProvider($provider); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $this->providers[$provider]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $provider |
58
|
|
|
* @throws WrongProviderException |
59
|
|
|
*/ |
60
|
|
|
private function addProvider($provider) |
61
|
|
|
{ |
62
|
|
|
$class = self::PROVIDERS_NAMESPACE.ucfirst($provider); |
63
|
|
|
|
64
|
|
|
if ( ! class_exists($class)) { |
65
|
|
|
throw new WrongProviderException; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// Create a reflection of the called class |
69
|
|
|
$ref = new ReflectionClass($class); |
70
|
|
|
$obj = $ref->newInstanceArgs([$this->request, $this->response]); |
71
|
|
|
|
72
|
|
|
$this->providers[$provider] = $obj; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return RequestInterface |
77
|
|
|
*/ |
78
|
|
|
public function getRequest() |
79
|
|
|
{ |
80
|
|
|
return $this->request; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return ResponseInterface |
85
|
|
|
*/ |
86
|
|
|
public function getResponse() |
87
|
|
|
{ |
88
|
|
|
return $this->response; |
89
|
|
|
} |
90
|
|
|
} |