Completed
Pull Request — master (#581)
by
unknown
02:40
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
 * @copyright 2016
22
 *
23
 * @see      https://github.com/overtrue
24
 * @see      http://overtrue.me
25
 */
26
27
namespace EasyWeChat\OpenPlatform;
28
29
use EasyWeChat\Core\Exceptions\InvalidArgumentException;
30
use EasyWeChat\OpenPlatform\Traits\VerifyTicketTrait;
31
use EasyWeChat\Server\Guard as ServerGuard;
32
use EasyWeChat\Support\Arr;
33
use Symfony\Component\HttpFoundation\Request;
34
35
class Guard extends ServerGuard
36
{
37
    use VerifyTicketTrait;
38
39
    /**
40
     * Wechat push event types.
41
     *
42
     * @var array
43
     */
44
    protected $eventTypeMappings = [
45
        'authorized' => EventHandlers\Authorized::class,
46
        'unauthorized' => EventHandlers\Unauthorized::class,
47
        'updateauthorized' => EventHandlers\UpdateAuthorized::class,
48
        'component_verify_ticket' => EventHandlers\ComponentVerifyTicket::class,
49
    ];
50
51
    /**
52
     * Guard constructor.
53
     *
54
     * @param string       $token
55
     * @param VerifyTicket $ticketTrait
56
     * @param Request|null $request
57
     */
58
    public function __construct($token, VerifyTicket $ticketTrait, Request $request = null)
59
    {
60
        parent::__construct($token, $request);
61
62
        $this->setVerifyTicket($ticketTrait);
63
    }
64
65
    /**
66
     * Return for laravel-wechat.
67
     *
68
     * @return array
69
     */
70
    public function listServe()
71
    {
72
        $class = $this->getHandleClass();
73
74
        $message = $this->getCollectedMessage();
75
76
        call_user_func([new $class($this->verifyTicket), 'handle'], $message);
77
78
        return [
79
            $message->get('InfoType'), $message,
80
        ];
81
    }
82
83
    /**
84
     * Listen for wechat push event.
85
     *
86
     * @param callable|null $callback
87
     *
88
     * @return mixed
89
     *
90
     * @throws InvalidArgumentException
91
     */
92
    public function listen($callback = null)
93
    {
94
        $message = $this->getCollectedMessage();
95
96
        $class = $this->getHandleClass();
97
98
        if (is_callable($callback)) {
99
            $callback(
100
                call_user_func([new $class($this->verifyTicket), 'forward'], $message)
101
            );
102
        }
103
104
        return call_user_func([new $class($this->verifyTicket), 'handle'], $message);
105
    }
106
107
    /**
108
     * Get handler class.
109
     *
110
     * @return \EasyWeChat\OpenPlatform\EventHandlers\EventHandler
111
     *
112
     * @throws InvalidArgumentException
113
     */
114
    private function getHandleClass()
115
    {
116
        $message = $this->getCollectedMessage();
117
118
        $type = $message->get('InfoType');
119
120
        if (!$class = Arr::get($this->eventTypeMappings, $type)) {
121
            throw new InvalidArgumentException("Event Info Type \"$type\" does not exists.");
122
        }
123
124
        return $class;
125
    }
126
}
127