Passed
Push — dev ( 9df85f...3fd410 )
by Nico
16:01
created

UserInformation::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 5
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Stu\Module\Maintenance;
4
5
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...
6
use Stu\Module\Control\StuTime;
7
use Stu\Module\Message\Lib\PrivateMessageFolderTypeEnum;
8
use Stu\Module\Message\Lib\PrivateMessageSenderInterface;
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
use Stu\Orm\Entity\UserInterface;
11
use Stu\Orm\Repository\UserRepositoryInterface;
12
13
final class UserInformation implements MaintenanceHandlerInterface
14
{
15
    private const FOUR_MONTHS_IN_SECONDS = 4 * 30 * 24 * 60 * 60; // 4 Monate
16
    private const FIVE_MONTHS_IN_SECONDS = 5 * 30 * 24 * 60 * 60; // 5 Monate
17
    private const ONE_DAY_IN_SECONDS = 24 * 60 * 60;
18
19 1
    public function __construct(
20
        private UserRepositoryInterface $userRepository,
21
        private PrivateMessageSenderInterface $privateMessageSender,
22
        private StuTime $stuTime
23 1
    ) {}
24
25
    #[Override]
26
    public function handle(): void
27
    {
28
        $currentTime = $this->stuTime->time();
29
30
        $fourMonthsAgo = $currentTime - self::FOUR_MONTHS_IN_SECONDS;
31
        $fourMonthsPlusOneDayAgo = $currentTime - self::FOUR_MONTHS_IN_SECONDS - self::ONE_DAY_IN_SECONDS;
32
33
        $fiveMonthsAgo = $currentTime - self::FIVE_MONTHS_IN_SECONDS;
34
        $fiveMonthsPlusOneDayAgo = $currentTime - self::FIVE_MONTHS_IN_SECONDS - self::ONE_DAY_IN_SECONDS;
35
36
        foreach ($this->userRepository->getNonNpcList() as $user) {
37
            $creationTime = $user->getCreationDate();
38
39
            if ($creationTime <= $fourMonthsAgo && $creationTime > $fourMonthsPlusOneDayAgo) {
40
                $noobzoneLayerNames = $this->getNoobzoneLayerNames($user);
41
42
                if (!empty($noobzoneLayerNames)) {
43
                    $layerNamesString = implode(', ', $noobzoneLayerNames);
44
45
                    $message = sprintf(
46
                        "Du bist jetzt 4 Monate bei STU dabei. Deine Großmacht gestattet dir noch einen Monat neue Kolonien im Newbie Gebiet %s zu gründen. Such dir vielleicht schonmal Kolonien auf einer anderen Karte. Deine Kolonien bleiben natürlich auch danach bestehen, solang du sie nicht aufgibst.",
47
                        $layerNamesString
48
                    );
49
50
                    $this->privateMessageSender->send(
51
                        UserEnum::USER_NOONE,
52
                        $user->getId(),
53
                        $message,
54
                        PrivateMessageFolderTypeEnum::SPECIAL_MAIN
55
                    );
56
                }
57
            }
58
59
            if ($creationTime <= $fiveMonthsAgo && $creationTime > $fiveMonthsPlusOneDayAgo) {
60
                $noobzoneLayerNames = $this->getNoobzoneLayerNames($user);
61
62
                if (!empty($noobzoneLayerNames)) {
63
                    $layerNamesString = implode(', ', $noobzoneLayerNames);
64
65
                    $message = sprintf(
66
                        "Du kannst ab sofort keine neuen Kolonien mehr im Newbie Gebiet %s kolonisieren. Deine bestehenden Kolonien bleiben erhalten, bis du sie aufgibst.",
67
                        $layerNamesString
68
                    );
69
70
                    $this->privateMessageSender->send(
71
                        UserEnum::USER_NOONE,
72
                        $user->getId(),
73
                        $message,
74
                        PrivateMessageFolderTypeEnum::SPECIAL_MAIN
75
                    );
76
                }
77
            }
78
        }
79
    }
80
81
    /**
82
     * @return array<string>
83
     */
84
    private function getNoobzoneLayerNames(UserInterface $user): array
85
    {
86
        $noobzoneLayerNames = [];
87
88
        foreach ($user->getColonies() as $colony) {
89
            $system = $colony->getSystem();
90
            $layer = $system->getLayer();
91
92
            if ($layer !== null && $layer->isNoobzone()) {
93
                $layerName = $layer->getName();
94
95
                if (!in_array($layerName, $noobzoneLayerNames, true)) {
96
                    $noobzoneLayerNames[] = $layerName;
97
                }
98
            }
99
        }
100
101
        return $noobzoneLayerNames;
102
    }
103
}
104