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

OpenPlatformServiceProvider   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

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

1 Method

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