Completed
Push — master ( 931b65...d2ebaa )
by Carlos
03:47
created

ServiceProviders/OpenPlatformServiceProvider.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
/**
13
 * OpenPlatformServiceProvider.php.
14
 *
15
 * Part of Overtrue\WeChat.
16
 *
17
 * For the full copyright and license information, please view the LICENSE
18
 * file that was distributed with this source code.
19
 *
20
 * @author    mingyoung <[email protected]>
21
 * @author    lixiao <[email protected]>
22
 * @copyright 2016
23
 *
24
 * @see      https://github.com/overtrue
25
 * @see      http://overtrue.me
26
 */
27
28
namespace EasyWeChat\Foundation\ServiceProviders;
29
30
use EasyWeChat\Encryption\Encryptor;
31
use EasyWeChat\Foundation\Application;
32
use EasyWeChat\OpenPlatform\AccessToken;
33
use EasyWeChat\OpenPlatform\Api\BaseApi;
34
use EasyWeChat\OpenPlatform\Api\PreAuthorization;
35
use EasyWeChat\OpenPlatform\Authorization;
36
use EasyWeChat\OpenPlatform\AuthorizerAccessToken;
37
use EasyWeChat\OpenPlatform\EventHandlers;
38
use EasyWeChat\OpenPlatform\Guard;
39
use EasyWeChat\OpenPlatform\OpenPlatform;
40
use EasyWeChat\OpenPlatform\VerifyTicket;
41
use Overtrue\Socialite\SocialiteManager as Socialite;
42
use Pimple\Container;
43
use Pimple\ServiceProviderInterface;
44
45
class OpenPlatformServiceProvider implements ServiceProviderInterface
46
{
47
    /**
48
     * Registers services on the given container.
49
     *
50
     * This method should only be used to configure services and parameters.
51
     * It should not get services.
52
     *
53
     * @param Container $pimple A container instance
54
     */
55
    public function register(Container $pimple)
56
    {
57
        $pimple['open_platform.verify_ticket'] = function ($pimple) {
58
            return new VerifyTicket(
59
                $pimple['config']['open_platform']['app_id'],
60
                $pimple['cache']
61
            );
62
        };
63
64 View Code Duplication
        $pimple['open_platform.access_token'] = function ($pimple) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
            return new AccessToken(
66
                $pimple['config']['open_platform']['app_id'],
67
                $pimple['config']['open_platform']['secret'],
68
                $pimple['open_platform.verify_ticket'],
69
                $pimple['cache']
70
            );
71
        };
72
73 View Code Duplication
        $pimple['open_platform.encryptor'] = function ($pimple) {
74
            return new Encryptor(
75
                $pimple['config']['open_platform']['app_id'],
76
                $pimple['config']['open_platform']['token'],
77
                $pimple['config']['open_platform']['aes_key']
78
            );
79
        };
80
81
        $pimple['open_platform'] = function ($pimple) {
82
            return new OpenPlatform($pimple);
83
        };
84
85
        $pimple['open_platform.server'] = function ($pimple) {
86
            $server = new Guard($pimple['config']['open_platform']['token']);
87
            $server->debug($pimple['config']['debug']);
88
            $server->setEncryptor($pimple['open_platform.encryptor']);
89
            $server->setHandlers([
90
                Guard::EVENT_AUTHORIZED => $pimple['open_platform.handlers.authorized'],
91
                Guard::EVENT_UNAUTHORIZED => $pimple['open_platform.handlers.unauthorized'],
92
                Guard::EVENT_UPDATE_AUTHORIZED => $pimple['open_platform.handlers.updateauthorized'],
93
                Guard::EVENT_COMPONENT_VERIFY_TICKET => $pimple['open_platform.handlers.component_verify_ticket'],
94
            ]);
95
96
            return $server;
97
        };
98
99
        $pimple['open_platform.pre_auth'] = $pimple['open_platform.pre_authorization'] = function ($pimple) {
100
            return new PreAuthorization(
101
                $pimple['open_platform.access_token'],
102
                $pimple['config']['open_platform']
103
            );
104
        };
105
106
        $pimple['open_platform.api'] = function ($pimple) {
107
            return new BaseApi(
108
                $pimple['open_platform.access_token'],
109
                $pimple['config']['open_platform']
110
            );
111
        };
112
113
        $pimple['open_platform.authorization'] = function ($pimple) {
114
            return new Authorization(
115
                $pimple['open_platform.api'],
116
                $pimple['config']['open_platform']['app_id'],
117
                $pimple['cache']
118
            );
119
        };
120
121
        $pimple['open_platform.authorizer_token'] = function ($pimple) {
122
            return new AuthorizerAccessToken(
123
                $pimple['config']['open_platform']['app_id'],
124
                $pimple['open_platform.authorization']
125
            );
126
        };
127
128
        // Authorization events handlers.
129
        $pimple['open_platform.handlers.component_verify_ticket'] = function ($pimple) {
130
            return new EventHandlers\ComponentVerifyTicket($pimple['open_platform.verify_ticket']);
131
        };
132
        $pimple['open_platform.handlers.authorized'] = function ($pimple) {
133
            return new EventHandlers\Authorized($pimple['open_platform.authorization']);
134
        };
135
        $pimple['open_platform.handlers.updateauthorized'] = function ($pimple) {
136
            return new EventHandlers\UpdateAuthorized($pimple['open_platform.authorization']);
137
        };
138
        $pimple['open_platform.handlers.unauthorized'] = function ($pimple) {
139
            return new EventHandlers\Unauthorized($pimple['open_platform.authorization']);
140
        };
141
142
        $pimple['open_platform.app'] = function ($pimple) {
143
            return new Application($pimple['config']->toArray());
144
        };
145
146
        // OAuth for OpenPlatform.
147
        $pimple['open_platform.oauth'] = function ($pimple) {
148
            $callback = $this->prepareCallbackUrl($pimple);
149
            $scopes = $pimple['config']->get('open_platform.oauth.scopes', []);
150
            $socialite = (new Socialite([
151
                'wechat_open' => [
152
                    'client_id' => $pimple['open_platform.authorization']->getAuthorizerAppId(),
153
                    'client_secret' => [
154
                        $pimple['open_platform.access_token']->getAppId(),
155
                        $pimple['open_platform.access_token']->getToken(),
156
                    ],
157
                    'redirect' => $callback,
158
                ],
159
            ]))->driver('wechat_open');
160
161
            if (!empty($scopes)) {
162
                $socialite->scopes($scopes);
163
            }
164
165
            return $socialite;
166
        };
167
    }
168
169
    /**
170
     * Prepare the OAuth callback url for wechat.
171
     *
172
     * @param Container $pimple
173
     *
174
     * @return string
175
     */
176 View Code Duplication
    private function prepareCallbackUrl($pimple)
177
    {
178
        $callback = $pimple['config']->get('oauth.callback');
179
        if (0 === stripos($callback, 'http')) {
180
            return $callback;
181
        }
182
        $baseUrl = $pimple['request']->getSchemeAndHttpHost();
183
184
        return $baseUrl.'/'.ltrim($callback, '/');
185
    }
186
}
187