Test Setup Failed
Pull Request — master (#606)
by
unknown
03:03
created

Guard::handleMessage()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

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