Completed
Pull Request — master (#553)
by
unknown
02:35
created

OpenPlatformServiceProvider::register()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 19
nc 1
nop 1
dl 0
loc 32
rs 8.8571
c 0
b 0
f 0
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
 * This file is part of the wechat.
16
 *
17
 * (c) mingyoung <[email protected]>
18
 *
19
 * This source file is subject to the MIT license that is bundled
20
 * with this source code in the file LICENSE.
21
 */
22
23
namespace EasyWeChat\Foundation\ServiceProviders;
24
25
use EasyWeChat\Encryption\Encryptor;
26
use EasyWeChat\OpenPlatform\AccessToken;
27
use EasyWeChat\OpenPlatform\Guard;
28
use EasyWeChat\OpenPlatform\OpenPlatform;
29
use Pimple\Container;
30
use Pimple\ServiceProviderInterface;
31
32
/**
33
 * Class BroadcastServiceProvider.
34
 */
35
class OpenPlatformServiceProvider implements ServiceProviderInterface
36
{
37
    /**
38
     * Registers services on the given container.
39
     *
40
     * This method should only be used to configure services and parameters.
41
     * It should not get services.
42
     *
43
     * @param Container $pimple A container instance
44
     */
45
    public function register(Container $pimple)
46
    {
47
        $pimple['open_platform_access_token'] = function ($pimple) {
48
            return new AccessToken(
49
                $pimple['config']['open_platform']['app_id'],
50
                $pimple['config']['open_platform']['secret'],
51
                $pimple['cache']
52
            );
53
        };
54
55
        $pimple['open_platform_encryptor'] = function ($pimple) {
56
            return new Encryptor(
57
                $pimple['config']['open_platform']['app_id'],
58
                $pimple['config']['open_platform']['token'],
59
                $pimple['config']['open_platform']['aes_key']
60
            );
61
        };
62
63
        $pimple['open_platform'] = function ($pimple) {
64
            $server = new Guard($pimple['config']['open_platform']['token']);
65
66
            $server->debug($pimple['config']['debug']);
67
68
            $server->setEncryptor($pimple['open_platform_encryptor']);
69
70
            return new OpenPlatform(
71
                $server,
72
                $pimple['open_platform_access_token'],
73
                $pimple['config']['open_platform']
74
            );
75
        };
76
    }
77
}
78