Passed
Push — dev ( 216c8f...156d5c )
by Janko
15:03
created

ExplorableStarMapItem::getTradepost()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

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