Completed
Push — master ( 20b9a2...cc53a9 )
by Carlos
06:51 queued 03:32
created

Guard   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 45.65%

Importance

Changes 0
Metric Value
dl 0
loc 139
ccs 21
cts 46
cp 0.4565
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setContainer() 0 4 1
A serve() 0 21 3
A listServe() 0 11 1
A listen() 0 8 2
C handleMessage() 0 30 7
A getDefaultHandler() 0 9 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
 * Guard.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\OpenPlatform;
29
30
use EasyWeChat\Core\Exceptions\InvalidArgumentException;
31
use EasyWeChat\Server\Guard as ServerGuard;
32
use EasyWeChat\Support\Collection;
33
use Pimple\Container;
34
use Symfony\Component\HttpFoundation\Response;
35
36
class Guard extends ServerGuard
37
{
38
    const EVENT_AUTHORIZED = 'authorized';
39
    const EVENT_UNAUTHORIZED = 'unauthorized';
40
    const EVENT_UPDATE_AUTHORIZED = 'updateauthorized';
41
    const EVENT_COMPONENT_VERIFY_TICKET = 'component_verify_ticket';
42
43
    /**
44
     * Container in the scope of the open platform authorization.
45
     *
46
     * @var Container
47
     */
48
    protected $container;
49
50
    /**
51
     * Sets the container for use of event handlers.
52
     *
53
     * @param Container $container
54
     *
55
     * @see getDefaultHandler()
56
     */
57 3
    public function setContainer(Container $container)
58
    {
59 3
        $this->container = $container;
60 3
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function serve()
66
    {
67
        // If sees the `auth_code` query parameter in the url, that is,
68
        // authorization is successful and it calls back, meanwhile, an
69
        // `authorized` event, which also includes the auth code, is sent
70
        // from WeChat, and that event will be handled.
71
        if ($this->request->get('auth_code')) {
72
            return new Response('success');
73
        }
74
75
        $message = $this->getMessage();
76
77
        $this->handleMessage($message);
78
79
        // Handle Messages.
80
        if (isset($message['MsgType'])) {
81
            return parent::serve();
82
        }
83
84
        return new Response('success');
85
    }
86
87
    /**
88
     * Return for laravel-wechat.
89
     *
90
     * @return array
91
     */
92 2
    public function listServe()
93
    {
94
        $message = $this->getMessage();
95
        $this->handleMessage($message);
96
97
        $message = new Collection($message);
98
99
        return [
100
            $message->get('InfoType'), $message,
101
        ];
102 2
    }
103
104
    /**
105
     * Listen for wechat push event.
106
     *
107
     * @param callable|null $callback
108
     *
109
     * @return mixed
110
     *
111
     * @throws InvalidArgumentException
112
     */
113
    public function listen($callback = null)
114
    {
115
        if ($callback) {
116
            $this->setMessageHandler($callback);
117
        }
118
119
        return $this->serve();
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125 1
    protected function handleMessage($message)
126
    {
127 1
        if (is_array($message)) {
128 1
            $message = new Collection($message);
129 1
        }
130
131 1
        if ($message->has('MsgType')) {
132
            return parent::handleMessage($message->toArray());
133
        }
134
135 1
        $handler = $this->getDefaultHandler($message->get('InfoType'));
136
137 1
        $result = $handler->handle($message);
138
139
        // To be compatible with previous version: merges the auth result while
140
        // keeping the original message.
141 1
        if (is_array($result) || $result instanceof Collection) {
142 1
            $message->merge($result);
143 1
        } else {
144
            if (!empty($result)) {
145
                $message->set('result', $result);
146
            }
147
        }
148
149 1
        if ($customHandler = $this->getMessageHandler()) {
150
            $customHandler($message);
151
        }
152
153 1
        return $result;
154
    }
155
156
    /**
157
     * Gets the default handler by the info type.
158
     *
159
     * @param $type
160
     *
161
     * @return EventHandlers\EventHandler
162
     *
163
     * @throws InvalidArgumentException
164
     */
165 2
    protected function getDefaultHandler($type)
166
    {
167 2
        $handler = $this->container->offsetGet("open_platform.handlers.{$type}");
168 2
        if (!$handler) {
169
            throw new InvalidArgumentException("EventHandler \"$type\" does not exists.");
170
        }
171
172 2
        return $handler;
173
    }
174
}
175