Passed
Push — dev ( 83c248...96a6f5 )
by Janko
10:53
created

MessageCollection::add()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Spacecraft\Lib\Message;
6
7
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Stu\Lib\Information\InformationInterface;
9
use Stu\Lib\Information\InformationWrapper;
10
use Stu\Module\PlayerSetting\Lib\UserEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Module\PlayerSetting\Lib\UserEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
12
final class MessageCollection implements MessageCollectionInterface
13
{
14
    /** @var MessageInterface[] */
15
    private array $messages = [];
16
17 5
    public function __construct(private MessageFactoryInterface $messageFactory) {}
18
19 5
    #[Override]
20
    public function add(MessageInterface $msg): void
21
    {
22 5
        if (!$msg->isEmpty()) {
23 4
            $this->messages[] = $msg;
24
        }
25
    }
26
27
    #[Override]
28
    public function addMessageBy(string $text, ?int $recipient = null): MessageInterface
29
    {
30
        $message = $this->messageFactory->createMessage(
31
            UserEnum::USER_NOONE,
32
            $recipient,
33
            [$text]
34
        );
35
36
        $this->add($message);
37
38
        return $message;
39
    }
40
41
    #[Override]
42
    public function addInformation(?string $information): InformationInterface
43
    {
44
        if ($information !== null) {
45
            $this->addMessageBy($information);
46
        }
47
48
        return $this;
49
    }
50
51
    #[Override]
52
    public function addInformationf(string $information, ...$args): InformationInterface
53
    {
54
        $this->addMessageBy(vsprintf($information, $args));
55
56
        return $this;
57
    }
58
59 1
    #[Override]
60
    public function getRecipientIds(): array
61
    {
62 1
        $recipientIds = [];
63
64 1
        foreach ($this->messages as $message) {
65 1
            if ($message->isEmpty()) {
66
                continue;
67
            }
68
69 1
            $recipientId = $message->getRecipientId();
70
71 1
            if ($recipientId === null || $recipientId === UserEnum::USER_NOONE) {
72 1
                continue;
73
            }
74
75 1
            if (!in_array($recipientId, $recipientIds)) {
76 1
                $recipientIds[] = $recipientId;
77
            }
78
        }
79
80 1
        return $recipientIds;
81
    }
82
83 4
    #[Override]
84
    public function getInformationDump(?int $userId = null): InformationWrapper
85
    {
86 4
        $result = new InformationWrapper();
87
88 4
        foreach ($this->messages as $message) {
89 3
            if ($message->isEmpty()) {
90
                continue;
91
            }
92
93
            if (
94 3
                $userId === null
95 1
                || $message->getSenderId() === $userId
96 1
                || $message->getRecipientId() === null
97 3
                || $message->getRecipientId() === $userId
98
            ) {
99 3
                $result->addInformationArray($message->getMessage());
100
            }
101
        }
102
103 4
        return $result;
104
    }
105
106 2
    #[Override]
107
    public function isEmpty(): bool
108
    {
109 2
        return $this->getInformationDump()->isEmpty();
110
    }
111
}
112