Passed
Push — master ( 0a21cd...0ad97c )
by Janko
05:01
created

MessageCollection::getRecipientIds()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 11
nc 5
nop 0
dl 0
loc 22
ccs 11
cts 11
cp 1
crap 6
rs 9.2222
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\UserConstants;
0 ignored issues
show
Bug introduced by
The type Stu\Module\PlayerSetting\Lib\UserConstants 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 13
    public function __construct(private MessageFactoryInterface $messageFactory) {}
18
19 10
    #[Override]
20
    public function add(MessageInterface $msg): void
21
    {
22 10
        $this->messages[] = $msg;
23
    }
24
25 4
    #[Override]
26
    public function addMessageBy(string|array $text, ?int $recipient = null): MessageInterface
27
    {
28 4
        $message = $this->messageFactory->createMessage(
29 4
            UserConstants::USER_NOONE,
30 4
            $recipient,
31 4
            is_array($text) ? $text : [$text]
0 ignored issues
show
introduced by
The condition is_array($text) is always true.
Loading history...
32 4
        );
33
34 4
        $this->add($message);
35
36 4
        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 8
    #[Override]
58
    public function getRecipientIds(): array
59
    {
60 8
        $recipientIds = [];
61
62 8
        foreach ($this->messages as $message) {
63 6
            if ($message->isEmpty()) {
64 4
                continue;
65
            }
66
67 6
            $recipientId = $message->getRecipientId();
68
69 6
            if ($recipientId === null || $recipientId === UserConstants::USER_NOONE) {
70 6
                continue;
71
            }
72
73 6
            if (!in_array($recipientId, $recipientIds)) {
74 6
                $recipientIds[] = $recipientId;
75
            }
76
        }
77
78 8
        return $recipientIds;
79
    }
80
81 12
    #[Override]
82
    public function getInformationDump(?int $userId = null): InformationWrapper
83
    {
84 12
        $result = new InformationWrapper();
85
86 12
        foreach ($this->messages as $message) {
87 9
            if ($message->isEmpty()) {
88 5
                continue;
89
            }
90
91
            if (
92 8
                $userId === null
93 6
                || $message->getSenderId() === $userId
94 5
                || $message->getRecipientId() === null
95 8
                || $message->getRecipientId() === $userId
96
            ) {
97 8
                $result->addInformationArray($message->getMessage());
98
            }
99
        }
100
101 12
        return $result;
102
    }
103
104 2
    #[Override]
105
    public function isEmpty(): bool
106
    {
107 2
        return $this->getInformationDump()->isEmpty();
108
    }
109
}
110