Passed
Pull Request — master (#1891)
by Janko
50:41 queued 25:19
created

LotteryFacade::getTicketsOfLastPeriod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Trade\Lib;
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\Component\Game\TimeConstants;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Game\TimeConstants 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...
9
use Stu\Module\Control\StuTime;
10
use Stu\Module\Message\Lib\PrivateMessageFolderTypeEnum;
11
use Stu\Module\Message\Lib\PrivateMessageSenderInterface;
12
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...
13
use Stu\Orm\Entity\UserInterface;
14
use Stu\Orm\Repository\LotteryTicketRepositoryInterface;
15
16
final class LotteryFacade implements LotteryFacadeInterface
17
{
18 4
    public function __construct(private LotteryTicketRepositoryInterface $lotteryTicketRepository, private PrivateMessageSenderInterface $privateMessageSender, private StuTime $stuTime) {}
19
20 1
    #[Override]
21
    public function createLotteryTicket(UserInterface $user, bool $sendPm): void
22
    {
23 1
        $ticket = $this->lotteryTicketRepository->prototype();
24 1
        $ticket->setUser($user);
25 1
        $ticket->setPeriod($this->getCurrentOrLastPeriod(false));
26 1
        $this->lotteryTicketRepository->save($ticket);
27
28 1
        if ($sendPm) {
29
            $this->privateMessageSender->send(
30
                UserEnum::USER_NPC_FERG,
31
                $user->getId(),
32
                'Du hast ein Gratislos für den aktuellen Lotteriezeitraum erhalten. Möge das Glück mit dir sein!',
33
                PrivateMessageFolderTypeEnum::SPECIAL_TRADE
34
            );
35
        }
36
    }
37
38 1
    #[Override]
39
    public function getTicketAmount(bool $isLastPeriod): int
40
    {
41 1
        return $this->lotteryTicketRepository->getAmountByPeriod(
42 1
            $this->getCurrentOrLastPeriod($isLastPeriod)
43 1
        );
44
    }
45
46 1
    #[Override]
47
    public function getTicketAmountByUser(int $userId, bool $isLastPeriod): int
48
    {
49 1
        return $this->lotteryTicketRepository->getAmountByPeriodAndUser(
50 1
            $this->getCurrentOrLastPeriod($isLastPeriod),
51 1
            $userId
52 1
        );
53
    }
54
55 1
    #[Override]
56
    public function getTicketsOfLastPeriod(): array
57
    {
58 1
        return $this->lotteryTicketRepository->getByPeriod($this->getCurrentOrLastPeriod(true));
59
    }
60
61 4
    private function getCurrentOrLastPeriod(bool $isLastPeriod): string
62
    {
63 4
        $time = $this->stuTime->time();
64 4
        if ($isLastPeriod) {
65 1
            $time -= TimeConstants::ONE_DAY_IN_SECONDS;
66
        }
67
68 4
        return sprintf(
69 4
            '%d.%s',
70 4
            (int)date("Y", $time) + StuTime::STU_YEARS_IN_FUTURE_OFFSET,
71 4
            date("m", $time)
72 4
        );
73
    }
74
}
75