Passed
Push — master ( 6a58e6...9adfdf )
by Nico
26:19 queued 10:20
created

MessageCollection   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Test Coverage

Coverage 61.9%

Importance

Changes 0
Metric Value
wmc 20
eloc 43
dl 0
loc 96
ccs 26
cts 42
cp 0.619
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 1 1
B getInformationDump() 0 21 7
A getRecipientIds() 0 22 6
A addMessageBy() 0 12 1
A addInformationf() 0 6 1
A addInformation() 0 8 2
A isEmpty() 0 4 1
A add() 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\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
        $this->messages[] = $msg;
23
    }
24
25
    #[Override]
26
    public function addMessageBy(string $text, ?int $recipient = null): MessageInterface
27
    {
28
        $message = $this->messageFactory->createMessage(
29
            UserEnum::USER_NOONE,
30
            $recipient,
31
            [$text]
32
        );
33
34
        $this->add($message);
35
36
        return $message;
37
    }
38
39
    #[Override]
40
    public function addInformation(?string $information): InformationInterface
41
    {
42
        if ($information !== null) {
43
            $this->addMessageBy($information);
44
        }
45
46
        return $this;
47
    }
48
49
    #[Override]
50
    public function addInformationf(string $information, ...$args): InformationInterface
51
    {
52
        $this->addMessageBy(vsprintf($information, $args));
53
54
        return $this;
55
    }
56
57 1
    #[Override]
58
    public function getRecipientIds(): array
59
    {
60 1
        $recipientIds = [];
61
62 1
        foreach ($this->messages as $message) {
63 1
            if ($message->isEmpty()) {
64
                continue;
65
            }
66
67 1
            $recipientId = $message->getRecipientId();
68
69 1
            if ($recipientId === null || $recipientId === UserEnum::USER_NOONE) {
70 1
                continue;
71
            }
72
73 1
            if (!in_array($recipientId, $recipientIds)) {
74 1
                $recipientIds[] = $recipientId;
75
            }
76
        }
77
78 1
        return $recipientIds;
79
    }
80
81 4
    #[Override]
82
    public function getInformationDump(?int $userId = null): InformationWrapper
83
    {
84 4
        $result = new InformationWrapper();
85
86 4
        foreach ($this->messages as $message) {
87 4
            if ($message->isEmpty()) {
88 1
                continue;
89
            }
90
91
            if (
92 3
                $userId === null
93 1
                || $message->getSenderId() === $userId
94 1
                || $message->getRecipientId() === null
95 3
                || $message->getRecipientId() === $userId
96
            ) {
97 3
                $result->addInformationArray($message->getMessage());
98
            }
99
        }
100
101 4
        return $result;
102
    }
103
104 2
    #[Override]
105
    public function isEmpty(): bool
106
    {
107 2
        return $this->getInformationDump()->isEmpty();
108
    }
109
}
110