Passed
Push — master ( 6b02b9...a180a6 )
by Nico
26:39 queued 18:43
created

ExplorableStarMapItem   A

Complexity

Total Complexity 38

Size/Duplication

Total Lines 171
Duplicated Lines 0 %

Test Coverage

Coverage 7.23%

Importance

Changes 0
Metric Value
eloc 76
c 0
b 0
f 0
dl 0
loc 171
ccs 6
cts 83
cp 0.0723
rs 9.36
wmc 38

14 Methods

Rating   Name   Duplication   Size   Complexity  
A setHide() 0 5 1
B getBorder() 0 23 8
A getTitle() 0 14 6
A getFieldStyle() 0 12 3
B getIcon() 0 16 7
A getTradepostTitle() 0 14 2
A getFieldId() 0 3 1
A getCx() 0 3 1
A getTradepost() 0 11 3
A getStringWithoutBbCode() 0 3 1
A __construct() 0 12 1
A getLayer() 0 3 1
A getHref() 0 3 2
A getCy() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Starmap\Lib;
6
7
use JBBCode\Parser;
8
use Stu\Component\Map\EncodedMapInterface;
9
use Stu\Orm\Entity\LayerInterface;
10
use Stu\Orm\Entity\TradePostInterface;
11
use Stu\Orm\Repository\TradePostRepositoryInterface;
12
13
class ExplorableStarMapItem implements ExplorableStarMapItemInterface
14
{
15
    private TradePostRepositoryInterface $tradePostRepository;
16
17
    private EncodedMapInterface $encodedMap;
18
19
    private Parser $bbCodeParser;
20
21
    private ?TradePostInterface $tradepost = null;
22
23
    private bool $hide = false;
24
25
    private ExploreableStarMapInterface $exploreableStarMap;
26
27
    private LayerInterface $layer;
28
29 1
    public function __construct(
30
        TradePostRepositoryInterface $tradePostRepository,
31
        EncodedMapInterface $encodedMap,
32
        Parser $bbCodeParser,
33
        ExploreableStarMapInterface $exploreableStarMap,
34
        LayerInterface $layer
35
    ) {
36 1
        $this->tradePostRepository = $tradePostRepository;
37 1
        $this->encodedMap = $encodedMap;
38 1
        $this->bbCodeParser = $bbCodeParser;
39 1
        $this->exploreableStarMap = $exploreableStarMap;
40 1
        $this->layer = $layer;
41
    }
42
43
    public function getCx(): int
44
    {
45
        return $this->exploreableStarMap->getCx();
46
    }
47
48
    public function getCy(): int
49
    {
50
        return $this->exploreableStarMap->getCy();
51
    }
52
53
    public function getFieldId(): int
54
    {
55
        return $this->exploreableStarMap->getFieldId();
56
    }
57
58
    public function getLayer(): LayerInterface
59
    {
60
        return $this->layer;
61
    }
62
63
    public function getTitle(): ?string
64
    {
65
        if ($this->hide === true) {
66
            return null;
67
        }
68
69
        $tradepost = $this->getTradepost();
70
71
        return sprintf(
72
            '%s%s%s%s',
73
            $tradepost !== null ? $this->getTradepostTitle($tradepost) : '',
74
            $tradepost !== null && $this->exploreableStarMap->getMapped() !== null ? ' über ' : '',
75
            $this->exploreableStarMap->getMapped() !== null ? $this->exploreableStarMap->getSystemName() . '-System' : '',
76
            $this->exploreableStarMap->getRegionDescription() ?? ''
77
        );
78
    }
79
80
    private function getTradepostTitle(TradePostInterface $tradepost): string
81
    {
82
        $licenseInfo = $tradepost->getLatestLicenseInfo();
83
84
        if ($licenseInfo === null) {
85
            return $this->getStringWithoutBbCode($tradepost->getName());
86
        }
87
88
        return sprintf(
89
            '%s (Lizenz für %d Tage: %d %s)',
90
            $this->getStringWithoutBbCode($tradepost->getName()),
91
            $licenseInfo->getDays(),
92
            $licenseInfo->getAmount(),
93
            $licenseInfo->getCommodity()->getName()
94
        );
95
    }
96
97
    private function getStringWithoutBbCode(string $string): string
98
    {
99
        return $this->bbCodeParser->parse($string)->getAsText();
100
    }
101
102
    public function getIcon(): ?string
103
    {
104
        if ($this->hide === true) {
105
            return null;
106
        }
107
108
        $tradepost = $this->getTradepost();
109
110
        if ($tradepost === null && $this->exploreableStarMap->getMapped() === null) {
111
            return null;
112
        }
113
114
        return sprintf(
115
            '%s%s',
116
            $tradepost !== null && !$tradepost->isNpcTradepost() ? 'tradepost' : '',
117
            $this->exploreableStarMap->getMapped() ? 'mapped' : ''
118
        );
119
    }
120
121
    public function getHref(): ?string
122
    {
123
        return $this->exploreableStarMap->getMapped() ? sprintf('database.php?SHOW_ENTRY=1&cat=7&ent=%d', $this->exploreableStarMap->getMapped()) : null;
124
    }
125
126
    private function getTradepost(): ?TradePostInterface
127
    {
128
        if ($this->exploreableStarMap->getTradePostId() === null) {
129
            return null;
130
        }
131
132
        if ($this->tradepost === null) {
133
            $this->tradepost = $this->tradePostRepository->find($this->exploreableStarMap->getTradePostId());
134
        }
135
136
        return $this->tradepost;
137
    }
138
139
140
    public function setHide(bool $hide): ExplorableStarMapItemInterface
141
    {
142
        $this->hide = $hide;
143
144
        return $this;
145
    }
146
147
    private function getBorder(): string
148
    {
149
        $borderType = $this->exploreableStarMap->getMapBorderType();
150
        if ($borderType === null) {
151
            if ($this->exploreableStarMap->getAdminRegion() === null && $this->exploreableStarMap->getInfluenceArea() !== null) {
152
                $influenceArea = $this->exploreableStarMap->getInfluenceArea();
153
                $base = $influenceArea->getBase();
154
                if ($base !== null) {
155
                    $user = $base->getUser();
156
                    $ally = $user->getAlliance();
157
158
                    if ($ally !== null && strlen($ally->getRgbCode()) > 0) {
159
                        return 'border: 1px solid ' . $ally->getRgbCode();
160
                    } elseif (strlen($user->getRgbCode()) > 0) {
161
                        return 'border: 1px solid ' . $user->getRgbCode();
162
                    }
163
                }
164
            }
165
        } else {
166
            return 'border: 1px solid ' . $borderType->getColor();
167
        }
168
169
        return '';
170
    }
171
172
    public function getFieldStyle(): string
173
    {
174
        if ($this->hide === true) {
175
            $imageUrl = '0.png';
176
        } else if ($this->layer->isEncoded()) {
177
            $imageUrl = $this->encodedMap->getEncodedMapPath($this->getFieldId(), $this->getLayer());
178
        } else {
179
            $imageUrl = sprintf('%d/%d.png', $this->getLayer()->getId(), $this->getFieldId());
180
        }
181
182
        $style = "background-image: url('assets/map/" . $imageUrl . "'); opacity:1;";
183
        return $style . $this->getBorder();
184
    }
185
}
186