Completed
Pull Request — master (#221)
by Sergey
02:10
created

ProvidersContainer::getHttpClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
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\Exceptions\WrongProvider;
8
use seregazhuk\PinterestBot\Api\Contracts\HttpClient;
9
use seregazhuk\PinterestBot\Api\Providers\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->request, $this->response]);
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
        return isset($error['message']) ? $error['message'] : null;
111
    }
112
113
    /**
114
     * Simply proxies call to Response object.
115
     *
116
     * @return array|null
117
     */
118
    public function getClientInfo()
119
    {
120
        return $this->response->getClientInfo();
121
    }
122
123
    /**
124
     * Returns HttpClient object for setting user-agent string or
125
     * other CURL available options.
126
     *
127
     * @return HttpClient
128
     */
129
    public function getHttpClient()
130
    {
131
        return $this->request->getHttpClient();
132
    }
133
134
    /**
135
     * @param $provider
136
     * @return string
137
     * @throws WrongProvider
138
     */
139
    protected function resolveProviderClass($provider)
140
    {
141
        $className = self::PROVIDERS_NAMESPACE . ucfirst($provider);
142
143
        if (!class_exists($className)) {
144
            throw new WrongProvider("Provider $className not found.");
145
        }
146
147
        return $className;
148
    }
149
}
150