Event::getEventKey()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: hugh.li
5
 * Date: 2022/2/24
6
 * Time: 23:11
7
 */
8
9
namespace HughCube\Laravel\WeChat\Message\Event;
10
11
use Carbon\Carbon;
12
13
class Event implements \HughCube\Laravel\WeChat\Contracts\Message\Event\Event
14
{
15
    /**
16
     * @var array
17
     */
18
    protected $message;
19
20
    /**
21
     * @param  array  $message
22
     */
23
    public function __construct(array $message = [])
24
    {
25
        $this->message = $message;
26
    }
27
28
    /**
29
     * @param  string|null  $key
30
     * @return array|mixed|null
31
     */
32
    public function getMessage(?string $key = null)
33
    {
34
        if (null === $key) {
35
            return $this->message;
36
        }
37
38
        return $this->message[$key] ?? null;
39
    }
40
41
    public function getToUserName(): ?string
42
    {
43
        return $this->getMessage('ToUserName');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getMessage('ToUserName') could return the type array which is incompatible with the type-hinted return null|string. Consider adding an additional type-check to rule them out.
Loading history...
44
    }
45
46
    public function getFromUserName(): ?string
47
    {
48
        return $this->getMessage('FromUserName');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getMessage('FromUserName') could return the type array which is incompatible with the type-hinted return null|string. Consider adding an additional type-check to rule them out.
Loading history...
49
    }
50
51
    public function getMessageType(): ?string
52
    {
53
        return $this->getMessage('MsgType');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getMessage('MsgType') could return the type array which is incompatible with the type-hinted return null|string. Consider adding an additional type-check to rule them out.
Loading history...
54
    }
55
56
    public function getCreatedAt(): ?Carbon
57
    {
58
        $timestamp = $this->getMessage('CreateTime');
59
        if (empty($timestamp)) {
60
            return null;
61
        }
62
        return Carbon::createFromTimestamp($timestamp);
0 ignored issues
show
Bug introduced by
It seems like $timestamp can also be of type array; however, parameter $timestamp of Carbon\Carbon::createFromTimestamp() does only seem to accept double|integer|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
        return Carbon::createFromTimestamp(/** @scrutinizer ignore-type */ $timestamp);
Loading history...
63
    }
64
65
    public function getMessageId(): ?int
66
    {
67
        return $this->getMessage('MsgId') ?: $this->getMessage('MsgID');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getMessage...is->getMessage('MsgID') could return the type array which is incompatible with the type-hinted return integer|null. Consider adding an additional type-check to rule them out.
Loading history...
68
    }
69
70
    public function getEvent(): ?string
71
    {
72
        return $this->getMessage('Event');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getMessage('Event') could return the type array which is incompatible with the type-hinted return null|string. Consider adding an additional type-check to rule them out.
Loading history...
73
    }
74
75
    public function getEventKey()
76
    {
77
        return $this->getMessage('EventKey');
78
    }
79
80
    public function getOpenID(): ?string
81
    {
82
        return $this->getFromUserName();
83
    }
84
}
85