Completed
Pull Request — master (#14)
by Carlos
02:49
created

SocialiteManager::getRequest()   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
/*
4
 * This file is part of the overtrue/socialite.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Overtrue\Socialite;
13
14
use Closure;
15
use InvalidArgumentException;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpFoundation\Session\Session;
18
19
/**
20
 * Class SocialiteManager.
21
 */
22
class SocialiteManager implements FactoryInterface
23
{
24
    /**
25
     * The configuration.
26
     *
27
     * @var \Overtrue\Socialite\Config
28
     */
29
    protected $config;
30
31
    /**
32
     * The request instance.
33
     *
34
     * @var \Symfony\Component\HttpFoundation\Request
35
     */
36
    protected $request;
37
38
    /**
39
     * The registered custom driver creators.
40
     *
41
     * @var array
42
     */
43
    protected $customCreators = [];
44
45
    /**
46
     * The initial drivers.
47
     *
48
     * @var array
49
     */
50
    protected $initialDrivers = [
51
            'facebook' => 'Facebook',
52
            'github'   => 'GitHub',
53
            'google'   => 'Google',
54
            'linkedin' => 'Linkedin',
55
            'weibo'    => 'Weibo',
56
            'qq'       => 'QQ',
57
            'wechat'   => 'WeChat',
58
            'douban'   => 'Douban',
59
    ];
60
61
    /**
62
     * The array of created "drivers".
63
     *
64
     * @var array
65
     */
66
    protected $drivers = [];
67
68
    /**
69
     * SocialiteManager constructor.
70
     *
71
     * @param array                                          $config
72
     * @param \Symfony\Component\HttpFoundation\Request|null $request
73
     */
74
    public function __construct(array $config, Request $request = null)
75
    {
76
        $this->config  = new Config($config);
77
        $this->request = $request ?: $this->createDefaultRequest();
78
    }
79
80
    /**
81
     * Set config instance.
82
     *
83
     * @param \Overtrue\Socialite\Config $config
84
     *
85
     * @return $this
86
     */
87
    public function config(Config $config)
88
    {
89
        $this->config = $config;
90
91
        return $this;
92
    }
93
94
    /**
95
     * Get the default driver name.
96
     *
97
     * @return string
98
     */
99
    public function getDefaultDriver()
100
    {
101
        throw new InvalidArgumentException('No Socialite driver was specified.');
102
    }
103
104
    /**
105
     * Get a driver instance.
106
     *
107
     * @param string $driver
108
     *
109
     * @return mixed
110
     */
111
    public function driver($driver = null)
112
    {
113
        $driver = $driver ?: $this->getDefaultDriver();
114
115
        if (!isset($this->drivers[$driver])) {
116
            $this->drivers[$driver] = $this->createDriver($driver);
117
        }
118
119
        return $this->drivers[$driver];
120
    }
121
122
    /**
123
     * Create a new driver instance.
124
     *
125
     * @param string $driver
126
     *
127
     * @return mixed
128
     *
129
     * @throws \InvalidArgumentException
130
     */
131
    protected function createDriver($driver)
132
    {
133
        if (isset($this->initialDrivers[$driver])) {
134
            $provider = $this->initialDrivers[$driver];
135
            $provider = __NAMESPACE__.'\\Providers\\'.$provider.'Provider';
136
137
            return $this->buildProvider($provider, $this->formatConfig($this->config->get($driver)));
138
        }
139
140
        if (isset($this->customCreators[$driver])) {
141
            return $this->callCustomCreator($driver);
142
        }
143
144
        throw new InvalidArgumentException("Driver [$driver] not supported.");
145
    }
146
147
    /**
148
     * Call a custom driver creator.
149
     *
150
     * @param string $driver
151
     *
152
     * @return mixed
153
     */
154
    protected function callCustomCreator($driver)
155
    {
156
        return $this->customCreators[$driver]($this->config);
157
    }
158
159
    /**
160
     * Create default request instance.
161
     *
162
     * @return \Symfony\Component\HttpFoundation\Request
163
     */
164
    protected function createDefaultRequest()
165
    {
166
        $request = Request::createFromGlobals();
167
        $session = new Session();
168
169
        $request->setSession($session);
170
171
        return $request;
172
    }
173
174
    /**
175
     * Register a custom driver creator Closure.
176
     *
177
     * @param string   $driver
178
     * @param \Closure $callback
179
     *
180
     * @return $this
181
     */
182
    public function extend($driver, Closure $callback)
183
    {
184
        $this->customCreators[$driver] = $callback;
185
186
        return $this;
187
    }
188
189
    /**
190
     * Get all of the created "drivers".
191
     *
192
     * @return array
193
     */
194
    public function getDrivers()
195
    {
196
        return $this->drivers;
197
    }
198
199
    /**
200
     * Get a driver instance.
201
     *
202
     * @param string $driver
203
     *
204
     * @return mixed
205
     */
206
    public function with($driver)
207
    {
208
        return $this->driver($driver);
209
    }
210
211
    /**
212
     * Build an OAuth 2 provider instance.
213
     *
214
     * @param string $provider
215
     * @param array  $config
216
     *
217
     * @return \Overtrue\Socialite\AbstractProvider
218
     */
219
    public function buildProvider($provider, $config)
220
    {
221
        return new $provider(
222
            $this->request, $config['client_id'],
223
            $config['client_secret'], $config['redirect']
224
        );
225
    }
226
227
    /**
228
     * Format the server configuration.
229
     *
230
     * @param array $config
231
     *
232
     * @return array
233
     */
234
    public function formatConfig(array $config)
235
    {
236
        return array_merge([
237
            'identifier'   => $config['client_id'],
238
            'secret'       => $config['client_secret'],
239
            'callback_uri' => $config['redirect'],
240
        ], $config);
241
    }
242
243
    /**
244
     * Dynamically call the default driver instance.
245
     *
246
     * @param string $method
247
     * @param array  $parameters
248
     *
249
     * @return mixed
250
     */
251
    public function __call($method, $parameters)
252
    {
253
        return call_user_func_array([$this->driver(), $method], $parameters);
254
    }
255
}
256