Completed
Push — master ( 19ca72...4bd58a )
by Kirill
02:15
created

AbstractMessage::getUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of Platform package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Karma\Platform\Io;
11
12
use Carbon\Carbon;
13
14
/**
15
 * Class AbstractMessage
16
 * @package Karma\Platform\Io
17
 */
18
abstract class AbstractMessage implements MessageInterface
19
{
20
    /**
21
     * Message edit timeout
22
     */
23
    protected const MESSAGE_EDIT_TIMEOUT = -1;
24
25
    /**
26
     * @var ChannelInterface
27
     */
28
    protected $channel;
29
30
    /**
31
     * @var UserInterface
32
     */
33
    protected $author;
34
35
    /**
36
     * @var string
37
     */
38
    protected $id;
39
40
    /**
41
     * @var string
42
     */
43
    protected $body;
44
45
    /**
46
     * @var array|UserInterface[]
47
     */
48
    protected $mentions = [];
49
50
    /**
51
     * @var \DateTimeInterface
52
     */
53
    protected $createdAt;
54
55
    /**
56
     * AbstractMessage constructor.
57
     * @param ChannelInterface $channel
58
     * @param UserInterface $author
59
     * @param string $id
60
     * @param string $body
61
     */
62
    public function __construct(ChannelInterface $channel, UserInterface $author, string $id, string $body)
63
    {
64
        $this->channel = $channel;
65
        $this->author = $author;
66
        $this->id = $id;
67
        $this->body = $body;
68
        $this->createdAt = Carbon::now();
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    public function getBody(): string
75
    {
76
        return $this->body;
77
    }
78
79
    /**
80
     * @return ChannelInterface
81
     */
82
    public function getChannel(): ChannelInterface
83
    {
84
        return $this->channel;
85
    }
86
87
    /**
88
     * @return UserInterface
89
     */
90
    public function getUser(): UserInterface
91
    {
92
        return $this->author;
93
    }
94
95
    /**
96
     * @return \Traversable
97
     */
98
    public function getMentions(): \Traversable
99
    {
100
        return new \ArrayIterator($this->mentions);
101
    }
102
103
    /**
104
     * @return \DateTimeInterface
105
     */
106
    public function at(): \DateTimeInterface
107
    {
108
        return $this->createdAt;
109
    }
110
111
    /**
112
     * @return string
113
     */
114
    public function getId(): string
115
    {
116
        return $this->id;
117
    }
118
119
    /**
120
     * @return bool
121
     */
122
    public function canBeUpdated(): bool
123
    {
124
        switch (static::MESSAGE_EDIT_TIMEOUT) {
125
            case 0:  return true;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
126
            case -1; return false;
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
127
        }
128
129
        return Carbon::createFromTimestamp($this->at()->getTimestamp(), $this->at()->getTimezone())
130
                ->addSeconds(static::MESSAGE_EDIT_TIMEOUT) > Carbon::now($this->at()->getTimezone());
131
    }
132
}
133