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
|
|
|
|
11
|
|
|
class ProvidersContainer |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var RequestInterface |
15
|
|
|
*/ |
16
|
|
|
protected $request; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var ResponseInterface |
20
|
|
|
*/ |
21
|
|
|
protected $response; |
22
|
|
|
|
23
|
|
|
const PROVIDERS_NAMESPACE = "seregazhuk\\PinterestBot\\Api\\Providers\\"; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* A array containing the cached providers |
27
|
|
|
* |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
private $providers = []; |
31
|
|
|
|
32
|
|
|
public function __construct(RequestInterface $request, ResponseInterface $response) |
33
|
|
|
{ |
34
|
|
|
$this->request = $request; |
35
|
|
|
$this->response = $response; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $provider |
40
|
|
|
* @return Provider |
41
|
|
|
* @throws WrongProviderException |
42
|
|
|
*/ |
43
|
|
|
public function getProvider($provider) |
44
|
|
|
{ |
45
|
|
|
// Check if an instance has already been initiated |
46
|
|
|
if (!isset($this->providers[$provider])) { |
47
|
|
|
$this->addProvider($provider); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $this->providers[$provider]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $provider |
55
|
|
|
* @throws WrongProviderException |
56
|
|
|
*/ |
57
|
|
|
private function addProvider($provider) |
58
|
|
|
{ |
59
|
|
|
$class = self::PROVIDERS_NAMESPACE . ucfirst($provider); |
60
|
|
|
|
61
|
|
|
if (!class_exists($class)) { |
62
|
|
|
throw new WrongProviderException; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// Create a reflection of the called class |
66
|
|
|
$ref = new ReflectionClass($class); |
67
|
|
|
$obj = $ref->newInstanceArgs([$this->request, $this->response]); |
68
|
|
|
|
69
|
|
|
$this->providers[$provider] = $obj; |
70
|
|
|
} |
71
|
|
|
} |