Completed
Pull Request — master (#170)
by Sergey
02:57
created

ProvidersContainer   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 126
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getProvider() 0 11 2
A addProvider() 0 10 2
A buildProvider() 0 7 1
A getLastError() 0 8 2
A getClientInfo() 0 4 1
A getHttpClient() 0 4 1
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
61
        if (!isset($this->providers[$provider])) {
62
            $this->addProvider($provider);
63
        }
64
65
        return $this->providers[$provider];
66
    }
67
68
    /**
69
     * Creates provider by class name, and if success saves
70
     * it to providers array. Provider class must be in PROVIDERS_NAMESPACE.
71
     *
72
     * @param string $provider
73
     *
74
     * @throws WrongProvider
75
     */
76
    protected function addProvider($provider)
77
    {
78
        $className = self::PROVIDERS_NAMESPACE.ucfirst($provider);
79
80
        if (!class_exists($className)) {
81
            throw new WrongProvider("Provider $className not found.");
82
        }
83
84
        $this->providers[$provider] = $this->buildProvider($className);
85
    }
86
87
    /**
88
     * Build Provider object with reflection API.
89
     *
90
     * @param string $className
91
     *
92
     * @throws WrongProvider
93
     *
94
     * @return object
95
     */
96
    protected function buildProvider($className)
97
    {
98
        $provider = (new ReflectionClass($className))
99
            ->newInstanceArgs([$this->request, $this->response]);
100
101
        return new ProviderWrapper($provider);
102
    }
103
104
    /**
105
     * Proxy method to Request object.
106
     *
107
     * @return string|null
108
     */
109
    public function getLastError()
110
    {
111
        $error = $this
112
            ->request
113
            ->getLastError();
114
115
        return isset($error['message']) ? $error['message'] : null;
116
    }
117
118
    /**
119
     * @return array|null
120
     */
121
    public function getClientInfo()
122
    {
123
        return $this->response->getClientInfo();
124
    }
125
126
    /**
127
     * Returns HttpClient object for setting user-agent string or
128
     * other CURL available options.
129
     *
130
     * @return HttpClient
131
     */
132
    public function getHttpClient()
133
    {
134
        return $this->request->getHttpClient();
135
    }
136
}
137