Passed
Push — master ( 92a2e9...9d73f4 )
by Nico
41:55
created

InitializeShowTransfer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 23
c 0
b 0
f 0
dl 0
loc 57
ccs 0
cts 29
cp 0
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setPageTitle() 0 10 4
A init() 0 25 1
A getTransferStrategy() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Lib\Transfer;
6
7
use RuntimeException;
8
use Stu\Component\Player\Relation\PlayerRelationDeterminatorInterface;
9
use Stu\Lib\Transfer\BeamUtilInterface;
10
use Stu\Lib\Transfer\Strategy\TransferStrategyInterface;
11
use Stu\Lib\Transfer\TransferInformation;
12
use Stu\Lib\Transfer\TransferTypeEnum;
13
use Stu\Module\Control\GameControllerInterface;
14
use Stu\Orm\Entity\ColonyInterface;
15
use Stu\Orm\Entity\ShipInterface;
16
17
final class InitializeShowTransfer implements InitializeShowTransferInterface
18
{
19
    /** @param array<TransferStrategyInterface> $transferStrategies */
20
    public function __construct(
21
        private BeamUtilInterface $beamUtil,
22
        private PlayerRelationDeterminatorInterface $playerRelationDeterminator,
23
        private array $transferStrategies
24
    ) {}
25
26
    public function init(
27
        ColonyInterface|ShipInterface $from,
28
        ColonyInterface|ShipInterface $to,
29
        bool $isUnload,
30
        TransferTypeEnum $transferType,
31
        GameControllerInterface $game
32
    ): void {
33
        $user = $game->getUser();
34
35
        $transferInformation = new TransferInformation(
36
            $transferType,
37
            $from,
38
            $to,
39
            $isUnload,
40
            $this->playerRelationDeterminator->isFriend($to->getUser(), $from->getUser())
41
        );
42
43
        $this->setPageTitle($transferInformation, $game);
44
45
        $game->setTemplateVar('TARGET', $to);
46
        $game->setTemplateVar('OWNS_TARGET', $to->getUser() === $user);
47
        $game->setTemplateVar('TRANSFER_INFO', $transferInformation);
48
49
        $strategy = $this->getTransferStrategy($transferType);
50
        $strategy->setTemplateVariables($isUnload, $from, $to, $game);
51
    }
52
53
54
    private function setPageTitle(
55
        TransferInformation $transferInformation,
56
        GameControllerInterface $game
57
    ): void {
58
        $game->setPageTitle(sprintf(
59
            '%s %s %s %s',
60
            $transferInformation->getTransferType()->getGoodName(),
61
            $transferInformation->isUnload() ? 'zu' : 'von',
62
            $transferInformation->isColonyTarget() ? 'Kolonie' : 'Schiff',
63
            $this->beamUtil->isDockTransfer($transferInformation->getSource(), $transferInformation->getTarget()) ? 'transferieren' : 'beamen'
64
        ));
65
    }
66
67
    private function getTransferStrategy(TransferTypeEnum $transferType): TransferStrategyInterface
68
    {
69
        if (!array_key_exists($transferType->value, $this->transferStrategies)) {
70
            throw new RuntimeException(sprintf('transfer strategy with typeValue %d does not exist', $transferType->value));
71
        }
72
73
        return $this->transferStrategies[$transferType->value];
74
    }
75
}
76