Completed
Push — master ( 9b99ba...c3f75f )
by Kirill
02:20
created

AbstractMessage::getMentions()   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
use Karma\Platform\Ast\NodeList;
14
use SebastianBergmann\CodeCoverage\Report\Xml\Node;
15
16
/**
17
 * Class AbstractMessage
18
 * @package Karma\Platform\Io
19
 */
20
abstract class AbstractMessage implements MessageInterface
21
{
22
    /**
23
     * Message edit timeout
24
     */
25
    protected const MESSAGE_EDIT_TIMEOUT = -1;
26
27
    /**
28
     * @var ChannelInterface
29
     */
30
    protected $channel;
31
32
    /**
33
     * @var UserInterface
34
     */
35
    protected $author;
36
37
    /**
38
     * @var string
39
     */
40
    protected $id;
41
42
    /**
43
     * @var NodeList
44
     */
45
    protected $body;
46
47
    /**
48
     * @var array|UserInterface[]
49
     */
50
    protected $mentions = [];
51
52
    /**
53
     * @var \DateTimeInterface
54
     */
55
    protected $createdAt;
56
57
    /**
58
     * AbstractMessage constructor.
59
     * @param ChannelInterface $channel
60
     * @param UserInterface $author
61
     * @param string $id
62
     * @param NodeList $body
63
     */
64
    public function __construct(ChannelInterface $channel, UserInterface $author, string $id, NodeList $body)
65
    {
66
        $this->channel = $channel;
67
        $this->author = $author;
68
        $this->id = $id;
69
        $this->body = $body;
70
        $this->createdAt = Carbon::now();
71
    }
72
73
    /**
74
     * @return bool
75
     */
76
    public function canBeUpdated(): bool
77
    {
78
        switch (static::MESSAGE_EDIT_TIMEOUT) {
79
            case 0:
80
                return true;
81
            case -1:
82
                return false;
83
        }
84
85
        return Carbon::createFromTimestamp($this->at()->getTimestamp(), $this->at()->getTimezone())
86
                ->addSeconds(static::MESSAGE_EDIT_TIMEOUT) > Carbon::now($this->at()->getTimezone());
87
    }
88
89
    /**
90
     * @return \DateTimeInterface
91
     */
92
    public function at(): \DateTimeInterface
93
    {
94
        return $this->createdAt;
95
    }
96
97
    /**
98
     * @return array
99
     */
100
    public function __debugInfo(): array
101
    {
102
        return [
103
            'id'       => $this->getId(),
104
            'message'  => $this->getNodes()->getBody(),
105
            'from'     => $this->getUser(),
106
            'in'       => $this->getChannel(),
107
            'at'       => $this->at()->format(Carbon::RFC3339),
108
            'mentions' => iterator_to_array($this->getMentions()),
109
        ];
110
    }
111
112
    /**
113
     * @return string
114
     */
115
    public function getId(): string
116
    {
117
        return $this->id;
118
    }
119
120
    /**
121
     * @return NodeList
122
     */
123
    public function getNodes(): NodeList
124
    {
125
        return $this->body;
126
    }
127
128
    /**
129
     * @return UserInterface
130
     */
131
    public function getUser(): UserInterface
132
    {
133
        return $this->author;
134
    }
135
136
    /**
137
     * @return ChannelInterface
138
     */
139
    public function getChannel(): ChannelInterface
140
    {
141
        return $this->channel;
142
    }
143
144
    /**
145
     * @return \Traversable
146
     */
147
    public function getMentions(): \Traversable
148
    {
149
        return new \ArrayIterator($this->mentions);
150
    }
151
}
152