Completed
Push — master ( 615456...bf7129 )
by Sergey
31:30 queued 25:25
created

ProvidersContainer::getProvider()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api;
4
5
use ReflectionClass;
6
use seregazhuk\PinterestBot\Exceptions\WrongProvider;
7
use seregazhuk\PinterestBot\Api\Contracts\HttpClient;
8
use seregazhuk\PinterestBot\Api\Providers\Core\Provider;
9
use seregazhuk\PinterestBot\Api\Providers\Core\ProviderWrapper;
10
11
class ProvidersContainer
12
{
13
    const PROVIDERS_NAMESPACE = 'seregazhuk\\PinterestBot\\Api\\Providers\\';
14
15
    /**
16
     * References to the request that travels
17
     * through the application.
18
     *
19
     * @var Request
20
     */
21
    protected $request;
22
23
    /**
24
     * @var Response
25
     */
26
    protected $response;
27
28
    /**
29
     * A array containing the cached providers.
30
     *
31
     * @var array
32
     */
33
    protected $providers = [];
34
35
    /**
36
     * @param Request $request
37
     * @param Response $response
38
     */
39
    public function __construct(Request $request, Response $response)
40
    {
41
        $this->request = $request;
42
        $this->response = $response;
43
    }
44
45
    /**
46
     * Gets provider object by name. If there is no such provider
47
     * in providers array, it will try to create it, then save
48
     * it, and then return.
49
     *
50
     * @param string $provider
51
     *
52
     * @throws WrongProvider
53
     *
54
     * @return Provider
55
     */
56
    public function getProvider($provider)
57
    {
58
        $provider = strtolower($provider);
59
60
        // Check if an instance has already been initiated. If not
61
        // build it and then add to the providers array.
62
        if (!isset($this->providers[$provider])) {
63
            $this->addProvider($provider);
64
        }
65
66
        return $this->providers[$provider];
67
    }
68
69
    /**
70
     * Creates provider by class name, and if success saves
71
     * it to providers array. Provider class must exist in PROVIDERS_NAMESPACE.
72
     *
73
     * @param string $provider
74
     * @throws WrongProvider
75
     */
76
    protected function addProvider($provider)
77
    {
78
        $className = $this->resolveProviderClass($provider);
79
80
        $this->providers[$provider] = $this->buildProvider($className);
81
    }
82
83
    /**
84
     * Build Provider object with reflection API.
85
     *
86
     * @param string $className
87
     * @throws WrongProvider
88
     * @return object
89
     */
90
    protected function buildProvider($className)
91
    {
92
        $provider = (new ReflectionClass($className))
93
            ->newInstanceArgs([$this]);
94
95
        return new ProviderWrapper($provider);
96
    }
97
98
    /**
99
     * Proxies call to Request object and returns message from
100
     * the error object.
101
     *
102
     * @return string|null
103
     */
104
    public function getLastError()
105
    {
106
        $error = $this
107
            ->response
108
            ->getLastError();
109
110
        if(isset($error['message'])) return $error['message'];
111
112
        if(isset($error['code'])) return $error['code'];
113
114
        return null;
115
    }
116
117
    /**
118
     * Simply proxies call to Response object.
119
     *
120
     * @return array|null
121
     */
122
    public function getClientInfo()
123
    {
124
        return $this->response->getClientInfo();
125
    }
126
127
    /**
128
     * Returns HttpClient object for setting user-agent string or
129
     * other CURL available options.
130
     *
131
     * @return HttpClient
132
     */
133
    public function getHttpClient()
134
    {
135
        return $this->request->getHttpClient();
136
    }
137
138
    /**
139
     * @param $provider
140
     * @return string
141
     * @throws WrongProvider
142
     */
143
    protected function resolveProviderClass($provider)
144
    {
145
        $className = self::PROVIDERS_NAMESPACE . ucfirst($provider);
146
147
        if (!class_exists($className)) {
148
            throw new WrongProvider("Provider $className not found.");
149
        }
150
151
        return $className;
152
    }
153
154
    /**
155
     * @return Request
156
     */
157
    public function getRequest()
158
    {
159
        return $this->request;
160
    }
161
162
    /**
163
     * @return Response
164
     */
165
    public function getResponse()
166
    {
167
        return $this->response;
168
    }
169
}
170