Completed
Push — master ( 87209a...672fdc )
by Sergey
01:08
created

ProvidersContainer::getResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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