Completed
Pull Request — master (#553)
by
unknown
02:30
created

Guard   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 10
c 0
b 0
f 0
wmc 2
lcom 1
cbo 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A listenFor() 0 14 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
 * @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\Support\Arr;
31
use EasyWeChat\Server\Guard as ServerGuard;
32
33
class Guard extends ServerGuard
34
{
35
36
    /**
37
     * Wechat push event types.
38
     *
39
     * @var array
40
     */
41
    protected $eventTypeMappings = [
42
        'component_verify_ticket' => EventHandlers\ComponentVerifyTicket::class,
43
        'authorized' => EventHandlers\Authorized::class,
44
        'unauthorized' => EventHandlers\Unauthorized::class,
45
        'updateauthorized' => EventHandlers\UpdateAuthorized::class,
46
    ];
47
48
    /**
49
     * Listen for wechat push event.
50
     *
51
     * @param $type
52
     * @param callable $callback
53
     *
54
     * @return mixed
55
     *
56
     * @throws InvalidArgumentException
57
     */
58
    public function listenFor($type, callable $callback)
59
    {
60
        $message = $this->getCollectedMessage();
61
62
        if (!$class = Arr::get($this->eventTypeMappings, $type)) {
63
            throw new InvalidArgumentException("Event Info Type \"$type\" does not exists.");
64
        }
65
66
        $callback(
67
            call_user_func([new $class, 'forward'], $message)
68
        );
69
70
        return call_user_func([new $class, 'handle'], $message);
71
    }
72
}
73