Passed
Push — master ( 128fe0...fa7a2a )
by Nico
10:40
created

MessageCollection   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 74.29%

Importance

Changes 0
Metric Value
eloc 36
c 0
b 0
f 0
dl 0
loc 78
ccs 26
cts 35
cp 0.7429
rs 10
wmc 17

6 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 4 1
B getInformationDump() 0 21 7
A __construct() 0 1 1
A getRecipientIds() 0 22 6
A addInformation() 0 12 1
A isEmpty() 0 4 1
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\InformationWrapper;
9
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...
10
11
final class MessageCollection implements MessageCollectionInterface
12
{
13
    /** @var MessageInterface[] */
14
    private array $messages = [];
15
16 5
    public function __construct(private MessageFactoryInterface $messageFactory) {}
17
18 5
    #[Override]
19
    public function add(MessageInterface $msg): void
20
    {
21 5
        $this->messages[] = $msg;
22
    }
23
24
    #[Override]
25
    public function addInformation(string $text, ?int $recipient = null): MessageInterface
26
    {
27
        $message = $this->messageFactory->createMessage(
28
            UserEnum::USER_NOONE,
29
            $recipient,
30
            [$text]
31
        );
32
33
        $this->add($message);
34
35
        return $message;
36
    }
37
38 1
    #[Override]
39
    public function getRecipientIds(): array
40
    {
41 1
        $recipientIds = [];
42
43 1
        foreach ($this->messages as $message) {
44 1
            if ($message->isEmpty()) {
45
                continue;
46
            }
47
48 1
            $recipientId = $message->getRecipientId();
49
50 1
            if ($recipientId === null || $recipientId === UserEnum::USER_NOONE) {
51 1
                continue;
52
            }
53
54 1
            if (!in_array($recipientId, $recipientIds)) {
55 1
                $recipientIds[] = $recipientId;
56
            }
57
        }
58
59 1
        return $recipientIds;
60
    }
61
62 4
    #[Override]
63
    public function getInformationDump(?int $userId = null): InformationWrapper
64
    {
65 4
        $result = new InformationWrapper();
66
67 4
        foreach ($this->messages as $message) {
68 4
            if ($message->isEmpty()) {
69 1
                continue;
70
            }
71
72
            if (
73 3
                $userId === null
74 1
                || $message->getSenderId() === $userId
75 1
                || $message->getRecipientId() === null
76 3
                || $message->getRecipientId() === $userId
77
            ) {
78 3
                $result->addInformationArray($message->getMessage());
79
            }
80
        }
81
82 4
        return $result;
83
    }
84
85 2
    #[Override]
86
    public function isEmpty(): bool
87
    {
88 2
        return $this->getInformationDump()->isEmpty();
89
    }
90
}
91