Passed
Push — master ( f4068b...77f637 )
by Nico
40:27 queued 14:09
created

MessageCollection::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Ship\Lib\Message;
6
7
use Stu\Lib\InformationWrapper;
8
use Stu\Module\PlayerSetting\Lib\UserEnum;
9
10
final class MessageCollection implements MessageCollectionInterface
11
{
12
    /**
13
     * @var MessageInterface[]
14
     */
15
    private array $messages = [];
16
17 5
    public function add(MessageInterface $msg): void
18
    {
19 5
        $this->messages[] = $msg;
20
    }
21
22 2
    public function addMultiple(array $messages): void
23
    {
24 2
        foreach ($messages as $msg) {
25 2
            $this->messages[] = $msg;
26
        }
27
    }
28
29 1
    public function getRecipientIds(): array
30
    {
31 1
        $recipientIds = [];
32
33 1
        foreach ($this->messages as $message) {
34 1
            if ($message->isEmpty()) {
35
                continue;
36
            }
37
38 1
            $recipientId = $message->getRecipientId();
39
40 1
            if ($recipientId === null || $recipientId === UserEnum::USER_NOONE) {
41 1
                continue;
42
            }
43
44 1
            if (!in_array($recipientId, $recipientIds)) {
45 1
                $recipientIds[] = $recipientId;
46
            }
47
        }
48
49 1
        return $recipientIds;
50
    }
51
52 6
    public function getInformationDump(?int $userId = null): InformationWrapper
53
    {
54 6
        $result = new InformationWrapper();
55
56 6
        foreach ($this->messages as $message) {
57 6
            if ($message->isEmpty()) {
58 1
                continue;
59
            }
60
61
            if (
62 5
                $userId === null
63 1
                || $message->getSenderId() === $userId
64 1
                || $message->getRecipientId() === null
65 5
                || $message->getRecipientId() === $userId
66
            ) {
67 5
                $result->addInformationArray($message->getMessage());
68
            }
69
        }
70
71 6
        return $result;
72
    }
73
74 2
    public function isEmpty(): bool
75
    {
76 2
        return $this->getInformationDump()->isEmpty();
77
    }
78
}
79