Passed
Pull Request — master (#1801)
by Nico
23:50
created

PirateProtection::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 8
ccs 0
cts 4
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Trade\Action\PirateProtection;
6
7
use Stu\Exception\AccessViolation;
8
use Stu\Lib\Pirate\Component\PirateWrathManagerInterface;
9
use Stu\Module\Control\ActionControllerInterface;
10
use Stu\Module\Control\GameControllerInterface;
11
use Stu\Module\Trade\View\ShowDeals\ShowDeals;
12
use Stu\Orm\Repository\TradeLicenseRepositoryInterface;
13
14
final class PirateProtection implements ActionControllerInterface
15
{
16
    public const ACTION_IDENTIFIER = 'B_PIRATE_PROTECTION';
17
18
    private TradeLicenseRepositoryInterface $tradeLicenseRepository;
19
    private PirateWrathManagerInterface $pirateWrathManager;
20
21
    private PirateProtectionRequestInterface $pirateProtectionRequest;
22
23
    public function __construct(
24
        TradeLicenseRepositoryInterface $tradeLicenseRepository,
25
        PirateWrathManagerInterface $pirateWrathManager,
26
        PirateProtectionRequestInterface $pirateProtectionRequest
27
    ) {
28
        $this->tradeLicenseRepository = $tradeLicenseRepository;
29
        $this->pirateWrathManager = $pirateWrathManager;
30
        $this->pirateProtectionRequest = $pirateProtectionRequest;
31
    }
32
33
    public function handle(GameControllerInterface $game): void
34
    {
35
        $userId = $game->getUser()->getId();
36
        $user = $game->getUser();
37
        $game->setView(ShowDeals::VIEW_IDENTIFIER);
38
39
        $prestige = $this->pirateProtectionRequest->getPrestige();
40
41
        if ($prestige < 1) {
42
            $game->addInformation(_('Mindestens 1 Prestige ist erforderlich'));
43
            return;
44
        }
45
46
        if ($prestige > $user->getPrestige()) {
47
            $game->addInformation(sprintf(
48
                _('Nicht genügend Prestige vorhanden. Du hast nur %d Prestige'),
49
                $user->getPrestige()
50
            ));
51
            return;
52
        }
53
54
        if (!$this->tradeLicenseRepository->hasFergLicense($userId)) {
55
            throw new AccessViolation(sprintf(
56
                _('UserId %d does not have license for Pirate Protection'),
57
                $userId
58
            ));
59
        }
60
61
        $this->pirateWrathManager->setProtectionTimeoutFromPrestige($user, $prestige, $game);
62
    }
63
64
    public function performSessionCheck(): bool
65
    {
66
        return true;
67
    }
68
}
69