Passed
Push — master ( 82014c...b13358 )
by Carlos
02:25
created

ServiceProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 41
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 18 2
A prepareCallbackUrl() 0 11 2
1
<?php
2
3
/*
4
 * This file is part of the overtrue/wechat.
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 EasyWeChat\Work\OAuth;
13
14
use Overtrue\Socialite\SocialiteManager;
15
use Pimple\Container;
16
use Pimple\ServiceProviderInterface;
17
18
class ServiceProvider implements ServiceProviderInterface
19
{
20
    public function register(Container $app)
21
    {
22
        $app['oauth'] = function ($app) {
23
            $socialite = (new SocialiteManager([
24
                'wework' => [
25
                    'client_id' => $app['config']['corp_id'],
26
                    'client_secret' => null,
27
                    'redirect' => $this->prepareCallbackUrl($app),
28
                ],
29
            ], $app['request']))->driver('wework');
30
31
            $scopes = (array) $app['config']->get('oauth.scopes', ['snsapi_base']);
32
33
            if (!empty($scopes)) {
34
                $socialite->scopes($scopes);
35
            }
36
37
            return $socialite->setAccessToken(new AccessTokenDelegate($app));
38
        };
39
    }
40
41
    /**
42
     * Prepare the OAuth callback url for wechat.
43
     *
44
     * @param Container $app
45
     *
46
     * @return string
47
     */
48
    private function prepareCallbackUrl($app)
49
    {
50
        $callback = $app['config']->get('oauth.callback');
51
52
        if (0 === stripos($callback, 'http')) {
53
            return $callback;
54
        }
55
56
        $baseUrl = $app['request']->getSchemeAndHttpHost();
57
58
        return $baseUrl.'/'.ltrim($callback, '/');
59
    }
60
}
61