Completed
Pull Request — master (#553)
by
unknown
03:27 queued 54s
created

OpenPlatformServiceProvider   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 10
c 0
b 0
f 0
wmc 2
lcom 0
cbo 4

1 Method

Rating   Name   Duplication   Size   Complexity  
B register() 0 36 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
/**
13
 * BroadcastServiceProvider.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
        $isEnableOpenPlatform = (bool) $pimple['config']['open_platform'];
48
49
        if ($isEnableOpenPlatform) {
50
            $pimple['open_platform_access_token'] = function ($pimple) {
51
                return new AccessToken(
52
                    $pimple['config']['open_platform']['app_id'],
53
                    $pimple['config']['open_platform']['secret'],
54
                    $pimple['cache']
55
                );
56
            };
57
58
            $pimple['open_platform_encryptor'] = function ($pimple) {
59
                return new Encryptor(
60
                    $pimple['config']['open_platform']['app_id'],
61
                    $pimple['config']['open_platform']['token'],
62
                    $pimple['config']['open_platform']['aes_key']
63
                );
64
            };
65
66
            $pimple['open_platform'] = function ($pimple) {
67
                $server = new Guard($pimple['config']['open_platform']['token']);
68
69
                $server->debug($pimple['config']['debug']);
70
71
                $server->setEncryptor($pimple['open_platform_encryptor']);
72
73
                return new OpenPlatform(
74
                    $server,
75
                    $pimple['open_platform_access_token'],
76
                    $pimple['config']['open_platform']
77
                );
78
            };
79
        }
80
    }
81
}
82