Passed
Push — 1.0.x ( 8353d5...3a2c37 )
by Julien
21:28
created

ServiceProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 11.11%

Importance

Changes 0
Metric Value
eloc 19
c 0
b 0
f 0
dl 0
loc 30
ccs 2
cts 18
cp 0.1111
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A register() 0 26 4
1
<?php
2
3
/**
4
 * This file is part of the Zemit Framework.
5
 *
6
 * (c) Zemit Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zemit\Provider\Oauth2Facebook;
13
14
use League\OAuth2\Client\Provider\Facebook;
15
use Phalcon\Di\DiInterface;
16
use Phalcon\Session\Manager;
17
use Zemit\Config\ConfigInterface;
18
use Zemit\Http\Request;
19
use Zemit\Provider\AbstractServiceProvider;
20
21
/**
22
 * @link https://github.com/tegaphilip/padlock
23
 * @link https://oauth2.thephpleague.com/framework-integrations/
24
 */
25
class ServiceProvider extends AbstractServiceProvider
26
{
27
    protected string $serviceName = 'oauth2Facebook';
28
    
29 23
    public function register(DiInterface $di): void
30
    {
31 23
        $di->setShared($this->getName(), function () use ($di) {
32
    
33
            $config = $di->get('config');
34
            assert($config instanceof ConfigInterface);
35
    
36
            $session = $di->get('session');
37
            assert($session instanceof Manager);
38
    
39
            $request = $di->get('request');
40
            assert($request instanceof Request);
41
            
42
            $oauthConfig = $config->pathToArray('oauth2') ?? [];
43
            $oauthFacebookConfig = $oauthConfig['facebook'] ?? [];
44
            
45
            // Set the full url
46
            $secure = $request->isSecure();
47
            $scheme = $request->getScheme() . '://';
48
            $host = $request->getHttpHost();
49
            $port = $request->getPort();
50
            $defaultPort = $secure ? 443 : 80;
51
            $port = $port !== $defaultPort? ':' . $port : null;
52
            $oauthFacebookConfig['redirectUri'] = $scheme . $host . $port . ($oauthFacebookConfig['redirectUri'] ?: '');
53
            
54
            return new Facebook($oauthFacebookConfig);
55 23
        });
56
    }
57
}
58