Passed
Push — dev ( 4fa92a...15821c )
by Janko
14:26 queued 05:24
created

ExplorableStarMapItem::getIcon()   A

Complexity

Conditions 6
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6

Importance

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