Completed
Push — master ( 760626...81d11d )
by Carlos
02:46
created

Guard::listServe()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 9
cp 0
crap 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\OpenPlatform\Traits\VerifyTicket;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, EasyWeChat\OpenPlatform\VerifyTicket.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
31
use EasyWeChat\Server\Guard as ServerGuard;
32
use EasyWeChat\Support\Arr;
33
34
class Guard extends ServerGuard
35
{
36
    use VerifyTicket;
37
38
    /**
39
     * Wechat push event types.
40
     *
41
     * @var array
42
     */
43
    protected $eventTypeMappings = [
44
        'authorized' => EventHandlers\Authorized::class,
45
        'unauthorized' => EventHandlers\Unauthorized::class,
46
        'updateauthorized' => EventHandlers\UpdateAuthorized::class,
47
        'component_verify_ticket' => EventHandlers\ComponentVerifyTicket::class,
48
    ];
49
50
    /**
51
     * Return for laravel-wechat.
52
     *
53
     * @return array
54
     */
55
    public function listServe()
56
    {
57
        $class = $this->getHandleClass();
58
59
        $message = $this->getCollectedMessage();
60
61
        call_user_func([new $class($this->verifyTicket), 'handle'], $message);
62
63
        return [
64
            $message->get('InfoType'), $message,
65
        ];
66
    }
67
68
    /**
69
     * Listen for wechat push event.
70
     *
71
     * @param callable|null $callback
72
     *
73
     * @return mixed
74
     *
75
     * @throws InvalidArgumentException
76
     */
77
    public function listen($callback = null)
78
    {
79
        $message = $this->getCollectedMessage();
80
81
        $class = $this->getHandleClass();
82
83
        if (is_callable($callback)) {
84
            $callback(
85
                call_user_func([new $class($this->verifyTicket), 'forward'], $message)
86
            );
87
        }
88
89
        return call_user_func([new $class($this->verifyTicket), 'handle'], $message);
90
    }
91
92
    /**
93
     * Get handler class.
94
     *
95
     * @return \EasyWeChat\OpenPlatform\EventHandlers\EventHandler
96
     *
97
     * @throws InvalidArgumentException
98
     */
99
    private function getHandleClass()
100
    {
101
        $message = $this->getCollectedMessage();
102
103
        $type = $message->get('InfoType');
104
105
        if (!$class = Arr::get($this->eventTypeMappings, $type)) {
106
            throw new InvalidArgumentException("Event Info Type \"$type\" does not exists.");
107
        }
108
109
        return $class;
110
    }
111
}
112