Completed
Push — master ( 017947...8f5b55 )
by frey
13s
created

Guard::serve()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 29
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 3
nop 0
dl 0
loc 29
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace EntWeChat\Suite;
4
5
use EntWeChat\Server\Guard as ServerGuard;
6
use EntWeChat\Support\Collection;
7
use EntWeChat\Support\Log;
8
use Symfony\Component\HttpFoundation\Response;
9
10
class Guard extends ServerGuard
11
{
12
    const EVENT_CREATE_AUTH = 'create_auth';
13
    const EVENT_CANCEL_AUTH = 'cancel_auth';
14
    const EVENT_CHANGE_AUTH = 'change_auth';
15
    const EVENT_SUITE_TICKET = 'suite_ticket';
16
17
    /**
18
     * Event handlers.
19
     *
20
     * @var \EntWeChat\Support\Collection
21
     */
22
    protected $handlers;
23
24
    /**
25
     * Get handlers.
26
     *
27
     * @return \EntWeChat\Support\Collection
28
     */
29
    public function getHandlers()
30
    {
31
        return $this->handlers;
32
    }
33
34
    /**
35
     * Set handlers.
36
     *
37
     * @param array $handlers
38
     */
39
    public function setHandlers(array $handlers)
40
    {
41
        $this->handlers = new Collection($handlers);
42
43
        return $this;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function serve()
50
    {
51
        $message = $this->getMessage();
52
53
        // Handle Messages.
54
        if (isset($message['MsgType'])) {
55
            return parent::serve();
56
        }
57
58
        Log::debug('Suite Request received:', [
59
            'Method'   => $this->request->getMethod(),
60
            'URI'      => $this->request->getRequestUri(),
61
            'Query'    => $this->request->getQueryString(),
62
            'Protocal' => $this->request->server->get('SERVER_PROTOCOL'),
63
            'Content'  => $this->request->getContent(),
64
        ]);
65
66
        // If sees the `auth_code` query parameter in the url, that is,
67
        // authorization is successful and it calls back, meanwhile, an
68
        // `authorized` event, which also includes the auth code, is sent
69
        // from WeChat, and that event will be handled.
70
        if ($this->request->get('auth_code')) {
71
            return new Response(self::SUCCESS_EMPTY_RESPONSE);
72
        }
73
74
        $this->handleEventMessage($message);
75
76
        return new Response(self::SUCCESS_EMPTY_RESPONSE);
77
    }
78
79
    /**
80
     * Handle event message.
81
     *
82
     * @param array $message
83
     */
84
    protected function handleEventMessage(array $message)
85
    {
86
        Log::debug('Suite Event Message detail:', $message);
87
88
        $message = new Collection($message);
89
90
        $infoType = $message->get('InfoType');
91
92
        if ($handler = $this->getHandler($infoType)) {
93
            $handler->handle($message);
94
        } else {
95
            Log::notice("No existing handler for '{$infoType}'.");
96
        }
97
98
        if ($customHandler = $this->getMessageHandler()) {
99
            $customHandler($message);
100
        }
101
    }
102
103
    /**
104
     * Get handler.
105
     *
106
     * @param string $type
107
     *
108
     * @return \EntWeChat\Suite\EventHandlers\EventHandler|null
109
     */
110
    public function getHandler($type)
111
    {
112
        return $this->handlers->get($type);
113
    }
114
}
115