Passed
Push — dev ( 709bf3...12fc43 )
by Nico
10:02
created

ExplorableStarMapItem::getLayer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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