Test Setup Failed
Push — master ( d37a6a...512c0e )
by Carlos
03:19
created

Guard::getHandleClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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