@@ -24,246 +24,246 @@ |
||
24 | 24 | */ |
25 | 25 | class Hybridauth |
26 | 26 | { |
27 | - /** |
|
28 | - * Hybridauth config. |
|
29 | - * |
|
30 | - * @var array |
|
31 | - */ |
|
32 | - protected $config; |
|
33 | - |
|
34 | - /** |
|
35 | - * Storage. |
|
36 | - * |
|
37 | - * @var StorageInterface |
|
38 | - */ |
|
39 | - protected $storage; |
|
40 | - |
|
41 | - /** |
|
42 | - * HttpClient. |
|
43 | - * |
|
44 | - * @var HttpClientInterface |
|
45 | - */ |
|
46 | - protected $httpClient; |
|
47 | - |
|
48 | - /** |
|
49 | - * Logger. |
|
50 | - * |
|
51 | - * @var LoggerInterface |
|
52 | - */ |
|
53 | - protected $logger; |
|
54 | - |
|
55 | - /** |
|
56 | - * @param array|string $config Array with configuration or Path to PHP file that will return array |
|
57 | - * @param HttpClientInterface $httpClient |
|
58 | - * @param StorageInterface $storage |
|
59 | - * @param LoggerInterface $logger |
|
60 | - * |
|
61 | - * @throws InvalidArgumentException |
|
62 | - */ |
|
63 | - public function __construct( |
|
64 | - $config, |
|
65 | - HttpClientInterface $httpClient = null, |
|
66 | - StorageInterface $storage = null, |
|
67 | - LoggerInterface $logger = null |
|
68 | - ) { |
|
69 | - if (is_string($config) && file_exists($config)) { |
|
70 | - $config = include $config; |
|
71 | - } elseif (!is_array($config)) { |
|
72 | - throw new InvalidArgumentException('Hybridauth config does not exist on the given path.'); |
|
73 | - } |
|
74 | - |
|
75 | - $this->config = $config + [ |
|
76 | - 'debug_mode' => Logger::NONE, |
|
77 | - 'debug_file' => '', |
|
78 | - 'curl_options' => null, |
|
79 | - 'providers' => [] |
|
80 | - ]; |
|
81 | - $this->storage = $storage; |
|
82 | - $this->logger = $logger; |
|
83 | - $this->httpClient = $httpClient; |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * Instantiate the given provider and authentication or authorization protocol. |
|
88 | - * |
|
89 | - * If not authenticated yet, the user will be redirected to the provider's site for |
|
90 | - * authentication/authorisation, otherwise it will simply return an instance of |
|
91 | - * provider's adapter. |
|
92 | - * |
|
93 | - * @param string $name adapter's name (case insensitive) |
|
94 | - * |
|
95 | - * @return \Hybridauth\Adapter\AdapterInterface |
|
96 | - * @throws InvalidArgumentException |
|
97 | - * @throws UnexpectedValueException |
|
98 | - */ |
|
99 | - public function authenticate($name) |
|
100 | - { |
|
101 | - $adapter = $this->getAdapter($name); |
|
102 | - |
|
103 | - $adapter->authenticate(); |
|
104 | - |
|
105 | - return $adapter; |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * Returns a new instance of a provider's adapter by name |
|
110 | - * |
|
111 | - * @param string $name adapter's name (case insensitive) |
|
112 | - * |
|
113 | - * @return \Hybridauth\Adapter\AdapterInterface |
|
114 | - * @throws InvalidArgumentException |
|
115 | - * @throws UnexpectedValueException |
|
116 | - */ |
|
117 | - public function getAdapter($name) |
|
118 | - { |
|
119 | - $config = $this->getProviderConfig($name); |
|
120 | - |
|
121 | - $adapter = isset($config['adapter']) ? $config['adapter'] : sprintf('Hybridauth\\Provider\\%s', $name); |
|
122 | - |
|
123 | - if (!class_exists($adapter)) { |
|
124 | - $unexistingConfiguredAdapter = $adapter; |
|
125 | - $adapter = null; |
|
126 | - $fs = new \FilesystemIterator(__DIR__ . '/Provider/'); |
|
127 | - /** @var \SplFileInfo $file */ |
|
128 | - foreach ($fs as $file) { |
|
129 | - if (!$file->isDir()) { |
|
130 | - $provider = strtok($file->getFilename(), '.'); |
|
131 | - if (mb_strtolower($name) === mb_strtolower($provider)) { |
|
132 | - $adapter = sprintf('Hybridauth\\Provider\\%s', $provider); |
|
133 | - break; |
|
134 | - } |
|
135 | - } |
|
136 | - } |
|
137 | - if ($adapter === null) { |
|
138 | - throw new InvalidArgumentException("Unknown Provider (name: $name / configured: $unexistingConfiguredAdapter)."); |
|
139 | - } |
|
140 | - } |
|
141 | - |
|
142 | - return new $adapter($config, $this->httpClient, $this->storage, $this->logger); |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * Get provider config by name. |
|
147 | - * |
|
148 | - * @param string $name adapter's name (case insensitive) |
|
149 | - * |
|
150 | - * @throws UnexpectedValueException |
|
151 | - * @throws InvalidArgumentException |
|
152 | - * |
|
153 | - * @return array |
|
154 | - */ |
|
155 | - public function getProviderConfig($name) |
|
156 | - { |
|
157 | - $name = strtolower($name); |
|
158 | - |
|
159 | - $providersConfig = array_change_key_case($this->config['providers'], CASE_LOWER); |
|
160 | - |
|
161 | - if (!isset($providersConfig[$name])) { |
|
162 | - throw new InvalidArgumentException('Unknown Provider.'); |
|
163 | - } |
|
164 | - |
|
165 | - if (!$providersConfig[$name]['enabled']) { |
|
166 | - throw new UnexpectedValueException('Disabled Provider.'); |
|
167 | - } |
|
168 | - |
|
169 | - $config = $providersConfig[$name]; |
|
170 | - $config += [ |
|
171 | - 'debug_mode' => $this->config['debug_mode'], |
|
172 | - 'debug_file' => $this->config['debug_file'], |
|
173 | - ]; |
|
174 | - |
|
175 | - if (!isset($config['callback']) && isset($this->config['callback'])) { |
|
176 | - $config['callback'] = $this->config['callback']; |
|
177 | - } |
|
178 | - |
|
179 | - return $config; |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * Returns a boolean of whether the user is connected with a provider |
|
184 | - * |
|
185 | - * @param string $name adapter's name (case insensitive) |
|
186 | - * |
|
187 | - * @return bool |
|
188 | - * @throws InvalidArgumentException |
|
189 | - * @throws UnexpectedValueException |
|
190 | - */ |
|
191 | - public function isConnectedWith($name) |
|
192 | - { |
|
193 | - return $this->getAdapter($name)->isConnected(); |
|
194 | - } |
|
195 | - |
|
196 | - /** |
|
197 | - * Returns a list of enabled adapters names |
|
198 | - * |
|
199 | - * @return array |
|
200 | - */ |
|
201 | - public function getProviders() |
|
202 | - { |
|
203 | - $providers = []; |
|
204 | - |
|
205 | - foreach ($this->config['providers'] as $name => $config) { |
|
206 | - if ($config['enabled']) { |
|
207 | - $providers[] = $name; |
|
208 | - } |
|
209 | - } |
|
210 | - |
|
211 | - return $providers; |
|
212 | - } |
|
213 | - |
|
214 | - /** |
|
215 | - * Returns a list of currently connected adapters names |
|
216 | - * |
|
217 | - * @return array |
|
218 | - * @throws InvalidArgumentException |
|
219 | - * @throws UnexpectedValueException |
|
220 | - */ |
|
221 | - public function getConnectedProviders() |
|
222 | - { |
|
223 | - $providers = []; |
|
224 | - |
|
225 | - foreach ($this->getProviders() as $name) { |
|
226 | - if ($this->isConnectedWith($name)) { |
|
227 | - $providers[] = $name; |
|
228 | - } |
|
229 | - } |
|
230 | - |
|
231 | - return $providers; |
|
232 | - } |
|
233 | - |
|
234 | - /** |
|
235 | - * Returns a list of new instances of currently connected adapters |
|
236 | - * |
|
237 | - * @return \Hybridauth\Adapter\AdapterInterface[] |
|
238 | - * @throws InvalidArgumentException |
|
239 | - * @throws UnexpectedValueException |
|
240 | - */ |
|
241 | - public function getConnectedAdapters() |
|
242 | - { |
|
243 | - $adapters = []; |
|
244 | - |
|
245 | - foreach ($this->getProviders() as $name) { |
|
246 | - $adapter = $this->getAdapter($name); |
|
247 | - |
|
248 | - if ($adapter->isConnected()) { |
|
249 | - $adapters[$name] = $adapter; |
|
250 | - } |
|
251 | - } |
|
252 | - |
|
253 | - return $adapters; |
|
254 | - } |
|
255 | - |
|
256 | - /** |
|
257 | - * Disconnect all currently connected adapters at once |
|
258 | - */ |
|
259 | - public function disconnectAllAdapters() |
|
260 | - { |
|
261 | - foreach ($this->getProviders() as $name) { |
|
262 | - $adapter = $this->getAdapter($name); |
|
263 | - |
|
264 | - if ($adapter->isConnected()) { |
|
265 | - $adapter->disconnect(); |
|
266 | - } |
|
267 | - } |
|
268 | - } |
|
27 | + /** |
|
28 | + * Hybridauth config. |
|
29 | + * |
|
30 | + * @var array |
|
31 | + */ |
|
32 | + protected $config; |
|
33 | + |
|
34 | + /** |
|
35 | + * Storage. |
|
36 | + * |
|
37 | + * @var StorageInterface |
|
38 | + */ |
|
39 | + protected $storage; |
|
40 | + |
|
41 | + /** |
|
42 | + * HttpClient. |
|
43 | + * |
|
44 | + * @var HttpClientInterface |
|
45 | + */ |
|
46 | + protected $httpClient; |
|
47 | + |
|
48 | + /** |
|
49 | + * Logger. |
|
50 | + * |
|
51 | + * @var LoggerInterface |
|
52 | + */ |
|
53 | + protected $logger; |
|
54 | + |
|
55 | + /** |
|
56 | + * @param array|string $config Array with configuration or Path to PHP file that will return array |
|
57 | + * @param HttpClientInterface $httpClient |
|
58 | + * @param StorageInterface $storage |
|
59 | + * @param LoggerInterface $logger |
|
60 | + * |
|
61 | + * @throws InvalidArgumentException |
|
62 | + */ |
|
63 | + public function __construct( |
|
64 | + $config, |
|
65 | + HttpClientInterface $httpClient = null, |
|
66 | + StorageInterface $storage = null, |
|
67 | + LoggerInterface $logger = null |
|
68 | + ) { |
|
69 | + if (is_string($config) && file_exists($config)) { |
|
70 | + $config = include $config; |
|
71 | + } elseif (!is_array($config)) { |
|
72 | + throw new InvalidArgumentException('Hybridauth config does not exist on the given path.'); |
|
73 | + } |
|
74 | + |
|
75 | + $this->config = $config + [ |
|
76 | + 'debug_mode' => Logger::NONE, |
|
77 | + 'debug_file' => '', |
|
78 | + 'curl_options' => null, |
|
79 | + 'providers' => [] |
|
80 | + ]; |
|
81 | + $this->storage = $storage; |
|
82 | + $this->logger = $logger; |
|
83 | + $this->httpClient = $httpClient; |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * Instantiate the given provider and authentication or authorization protocol. |
|
88 | + * |
|
89 | + * If not authenticated yet, the user will be redirected to the provider's site for |
|
90 | + * authentication/authorisation, otherwise it will simply return an instance of |
|
91 | + * provider's adapter. |
|
92 | + * |
|
93 | + * @param string $name adapter's name (case insensitive) |
|
94 | + * |
|
95 | + * @return \Hybridauth\Adapter\AdapterInterface |
|
96 | + * @throws InvalidArgumentException |
|
97 | + * @throws UnexpectedValueException |
|
98 | + */ |
|
99 | + public function authenticate($name) |
|
100 | + { |
|
101 | + $adapter = $this->getAdapter($name); |
|
102 | + |
|
103 | + $adapter->authenticate(); |
|
104 | + |
|
105 | + return $adapter; |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * Returns a new instance of a provider's adapter by name |
|
110 | + * |
|
111 | + * @param string $name adapter's name (case insensitive) |
|
112 | + * |
|
113 | + * @return \Hybridauth\Adapter\AdapterInterface |
|
114 | + * @throws InvalidArgumentException |
|
115 | + * @throws UnexpectedValueException |
|
116 | + */ |
|
117 | + public function getAdapter($name) |
|
118 | + { |
|
119 | + $config = $this->getProviderConfig($name); |
|
120 | + |
|
121 | + $adapter = isset($config['adapter']) ? $config['adapter'] : sprintf('Hybridauth\\Provider\\%s', $name); |
|
122 | + |
|
123 | + if (!class_exists($adapter)) { |
|
124 | + $unexistingConfiguredAdapter = $adapter; |
|
125 | + $adapter = null; |
|
126 | + $fs = new \FilesystemIterator(__DIR__ . '/Provider/'); |
|
127 | + /** @var \SplFileInfo $file */ |
|
128 | + foreach ($fs as $file) { |
|
129 | + if (!$file->isDir()) { |
|
130 | + $provider = strtok($file->getFilename(), '.'); |
|
131 | + if (mb_strtolower($name) === mb_strtolower($provider)) { |
|
132 | + $adapter = sprintf('Hybridauth\\Provider\\%s', $provider); |
|
133 | + break; |
|
134 | + } |
|
135 | + } |
|
136 | + } |
|
137 | + if ($adapter === null) { |
|
138 | + throw new InvalidArgumentException("Unknown Provider (name: $name / configured: $unexistingConfiguredAdapter)."); |
|
139 | + } |
|
140 | + } |
|
141 | + |
|
142 | + return new $adapter($config, $this->httpClient, $this->storage, $this->logger); |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * Get provider config by name. |
|
147 | + * |
|
148 | + * @param string $name adapter's name (case insensitive) |
|
149 | + * |
|
150 | + * @throws UnexpectedValueException |
|
151 | + * @throws InvalidArgumentException |
|
152 | + * |
|
153 | + * @return array |
|
154 | + */ |
|
155 | + public function getProviderConfig($name) |
|
156 | + { |
|
157 | + $name = strtolower($name); |
|
158 | + |
|
159 | + $providersConfig = array_change_key_case($this->config['providers'], CASE_LOWER); |
|
160 | + |
|
161 | + if (!isset($providersConfig[$name])) { |
|
162 | + throw new InvalidArgumentException('Unknown Provider.'); |
|
163 | + } |
|
164 | + |
|
165 | + if (!$providersConfig[$name]['enabled']) { |
|
166 | + throw new UnexpectedValueException('Disabled Provider.'); |
|
167 | + } |
|
168 | + |
|
169 | + $config = $providersConfig[$name]; |
|
170 | + $config += [ |
|
171 | + 'debug_mode' => $this->config['debug_mode'], |
|
172 | + 'debug_file' => $this->config['debug_file'], |
|
173 | + ]; |
|
174 | + |
|
175 | + if (!isset($config['callback']) && isset($this->config['callback'])) { |
|
176 | + $config['callback'] = $this->config['callback']; |
|
177 | + } |
|
178 | + |
|
179 | + return $config; |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * Returns a boolean of whether the user is connected with a provider |
|
184 | + * |
|
185 | + * @param string $name adapter's name (case insensitive) |
|
186 | + * |
|
187 | + * @return bool |
|
188 | + * @throws InvalidArgumentException |
|
189 | + * @throws UnexpectedValueException |
|
190 | + */ |
|
191 | + public function isConnectedWith($name) |
|
192 | + { |
|
193 | + return $this->getAdapter($name)->isConnected(); |
|
194 | + } |
|
195 | + |
|
196 | + /** |
|
197 | + * Returns a list of enabled adapters names |
|
198 | + * |
|
199 | + * @return array |
|
200 | + */ |
|
201 | + public function getProviders() |
|
202 | + { |
|
203 | + $providers = []; |
|
204 | + |
|
205 | + foreach ($this->config['providers'] as $name => $config) { |
|
206 | + if ($config['enabled']) { |
|
207 | + $providers[] = $name; |
|
208 | + } |
|
209 | + } |
|
210 | + |
|
211 | + return $providers; |
|
212 | + } |
|
213 | + |
|
214 | + /** |
|
215 | + * Returns a list of currently connected adapters names |
|
216 | + * |
|
217 | + * @return array |
|
218 | + * @throws InvalidArgumentException |
|
219 | + * @throws UnexpectedValueException |
|
220 | + */ |
|
221 | + public function getConnectedProviders() |
|
222 | + { |
|
223 | + $providers = []; |
|
224 | + |
|
225 | + foreach ($this->getProviders() as $name) { |
|
226 | + if ($this->isConnectedWith($name)) { |
|
227 | + $providers[] = $name; |
|
228 | + } |
|
229 | + } |
|
230 | + |
|
231 | + return $providers; |
|
232 | + } |
|
233 | + |
|
234 | + /** |
|
235 | + * Returns a list of new instances of currently connected adapters |
|
236 | + * |
|
237 | + * @return \Hybridauth\Adapter\AdapterInterface[] |
|
238 | + * @throws InvalidArgumentException |
|
239 | + * @throws UnexpectedValueException |
|
240 | + */ |
|
241 | + public function getConnectedAdapters() |
|
242 | + { |
|
243 | + $adapters = []; |
|
244 | + |
|
245 | + foreach ($this->getProviders() as $name) { |
|
246 | + $adapter = $this->getAdapter($name); |
|
247 | + |
|
248 | + if ($adapter->isConnected()) { |
|
249 | + $adapters[$name] = $adapter; |
|
250 | + } |
|
251 | + } |
|
252 | + |
|
253 | + return $adapters; |
|
254 | + } |
|
255 | + |
|
256 | + /** |
|
257 | + * Disconnect all currently connected adapters at once |
|
258 | + */ |
|
259 | + public function disconnectAllAdapters() |
|
260 | + { |
|
261 | + foreach ($this->getProviders() as $name) { |
|
262 | + $adapter = $this->getAdapter($name); |
|
263 | + |
|
264 | + if ($adapter->isConnected()) { |
|
265 | + $adapter->disconnect(); |
|
266 | + } |
|
267 | + } |
|
268 | + } |
|
269 | 269 | } |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | ) { |
69 | 69 | if (is_string($config) && file_exists($config)) { |
70 | 70 | $config = include $config; |
71 | - } elseif (!is_array($config)) { |
|
71 | + } elseif ( ! is_array($config)) { |
|
72 | 72 | throw new InvalidArgumentException('Hybridauth config does not exist on the given path.'); |
73 | 73 | } |
74 | 74 | |
@@ -120,13 +120,13 @@ discard block |
||
120 | 120 | |
121 | 121 | $adapter = isset($config['adapter']) ? $config['adapter'] : sprintf('Hybridauth\\Provider\\%s', $name); |
122 | 122 | |
123 | - if (!class_exists($adapter)) { |
|
123 | + if ( ! class_exists($adapter)) { |
|
124 | 124 | $unexistingConfiguredAdapter = $adapter; |
125 | 125 | $adapter = null; |
126 | - $fs = new \FilesystemIterator(__DIR__ . '/Provider/'); |
|
126 | + $fs = new \FilesystemIterator(__DIR__.'/Provider/'); |
|
127 | 127 | /** @var \SplFileInfo $file */ |
128 | 128 | foreach ($fs as $file) { |
129 | - if (!$file->isDir()) { |
|
129 | + if ( ! $file->isDir()) { |
|
130 | 130 | $provider = strtok($file->getFilename(), '.'); |
131 | 131 | if (mb_strtolower($name) === mb_strtolower($provider)) { |
132 | 132 | $adapter = sprintf('Hybridauth\\Provider\\%s', $provider); |
@@ -158,11 +158,11 @@ discard block |
||
158 | 158 | |
159 | 159 | $providersConfig = array_change_key_case($this->config['providers'], CASE_LOWER); |
160 | 160 | |
161 | - if (!isset($providersConfig[$name])) { |
|
161 | + if ( ! isset($providersConfig[$name])) { |
|
162 | 162 | throw new InvalidArgumentException('Unknown Provider.'); |
163 | 163 | } |
164 | 164 | |
165 | - if (!$providersConfig[$name]['enabled']) { |
|
165 | + if ( ! $providersConfig[$name]['enabled']) { |
|
166 | 166 | throw new UnexpectedValueException('Disabled Provider.'); |
167 | 167 | } |
168 | 168 | |
@@ -172,7 +172,7 @@ discard block |
||
172 | 172 | 'debug_file' => $this->config['debug_file'], |
173 | 173 | ]; |
174 | 174 | |
175 | - if (!isset($config['callback']) && isset($this->config['callback'])) { |
|
175 | + if ( ! isset($config['callback']) && isset($this->config['callback'])) { |
|
176 | 176 | $config['callback'] = $this->config['callback']; |
177 | 177 | } |
178 | 178 |