Passed
Pull Request — master (#1883)
by Janko
55:13 queued 27:46
created

ExplorableStarMapItem::getIcon()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

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