Passed
Push — dev ( f1dc2a...210bba )
by Nico
08:46
created

WeaponShield::getFactionId()   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
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Orm\Entity;
6
7
use Doctrine\ORM\Mapping\Column;
8
use Doctrine\ORM\Mapping\Entity;
9
use Doctrine\ORM\Mapping\GeneratedValue;
10
use Doctrine\ORM\Mapping\Id;
11
use Doctrine\ORM\Mapping\Index;
12
use Doctrine\ORM\Mapping\JoinColumn;
13
use Doctrine\ORM\Mapping\ManyToOne;
14
use Doctrine\ORM\Mapping\Table;
15
16
/**
17
 * @Entity(repositoryClass="Stu\Orm\Repository\WeaponShieldRepository")
18
 * @Table(
19
 *     name="stu_weapon_shield",
20
 *     indexes={
21
 *         @Index(name="weapon_shield_module_idx", columns={"module_id"}),
22
 *         @Index(name="weapon_shield_weapon_idx", columns={"weapon_id"})
23
 *     }
24
 * )
25
 **/
26
class WeaponShield implements WeaponShieldInterface
27
{
28
    /**
29
     * @Id
30
     * @Column(type="integer")
31
     * @GeneratedValue(strategy="IDENTITY")
32
     *
33
     */
34
    private int $id;
35
36
    /**
37
     * @Column(type="integer")
38
     *
39
     */
40
    private int $module_id = 0;
41
42
    /**
43
     * @Column(type="integer")
44
     *
45
     */
46
    private int $weapon_id = 0;
47
48
    /**
49
     * @Column(type="integer")
50
     *
51
     */
52
    private int $modificator = 0;
53
54
    /**
55
     * @Column(type="integer", nullable=true)
56
     * 
57
     */
58
    private int $faction_id = 0;
59
60
    /**
61
     * @ManyToOne(targetEntity="Weapon")
62
     * @JoinColumn(name="weapon_id", referencedColumnName="id")
63
     */
64
    private WeaponInterface $weapon;
65
66
    /**
67
     * @ManyToOne(targetEntity="Module")
68
     * @JoinColumn(name="module_id", referencedColumnName="id")
69
     */
70
    private ModuleInterface $module;
71
72
    public function getId(): int
73
    {
74
        return $this->id;
75
    }
76
77
    public function getModuleId(): int
78
    {
79
        return $this->module_id;
80
    }
81
82
    public function setModuleId(int $moduleId): WeaponShieldInterface
83
    {
84
        $this->module_id = $moduleId;
85
86
        return $this;
87
    }
88
89
    public function getWeaponId(): int
90
    {
91
        return $this->weapon_id;
92
    }
93
94
    public function setWeaponId(int $weaponid): WeaponShieldInterface
95
    {
96
        $this->weapon_id = $weaponid;
97
98
        return $this;
99
    }
100
101
    public function getModificator(): int
102
    {
103
        return $this->modificator;
104
    }
105
106
    public function setModificator(int $Modificator): WeaponShieldInterface
107
    {
108
        $this->modificator = $Modificator;
109
110
        return $this;
111
    }
112
113
    public function getFactionId(): int
114
    {
115
        return $this->faction_id;
116
    }
117
118
    public function setFactionId(int $factionid): WeaponShieldInterface
119
    {
120
        $this->faction_id = $factionid;
121
122
        return $this;
123
    }
124
125
    public function getWeapon(): WeaponInterface
126
    {
127
        return $this->weapon;
128
    }
129
130
    public function getModule(): ModuleInterface
131
    {
132
        return $this->module;
133
    }
134
135
    public function calculateGradientColor(): string
136
    {
137
        $color1 = '#00ff00';
138
        $color2 = '#ffd500';
139
        $color3 = '#FF0000';
140
        $percent = 100 / 20 * ($this->getModificator() - 100);
141
142
        // Konvertiere die Hex-Farbcodes in RGB-Werte
143
        $rgb1 = $this->hexToRgb($color1);
144
        $rgb2 = $this->hexToRgb($color2);
145
        $rgb3 = $this->hexToRgb($color3);
146
147
        // Verteile den Prozentwert zwischen den Farben
148
        if ($percent <= 50) {
149
            $gradientPercent = $percent * 2;
150
            $gradientRgb = $this->calculateGradientRgb($rgb1, $rgb2, $gradientPercent);
151
        } else {
152
            $gradientPercent = (($percent - 50) * 2);
153
            $gradientRgb = $this->calculateGradientRgb($rgb2, $rgb3, $gradientPercent);
154
        }
155
156
        // Konvertiere den RGB-Wert zurück in einen Hex-Farbcode
157
        $gradientColor = $this->rgbToHex($gradientRgb);
158
159
        return $gradientColor;
160
    }
161
    /**
162
     * @return array<int, int|float>
163
     */
164
    private function hexToRgb(string $color): array
165
    {
166
        $color = ltrim($color, '#');
167
        $length = strlen($color);
168
        $b = 0;
169
        $g = 0;
170
        $r = 0;
171
        if ($length == 3) {
172
            $r = hexdec(substr($color, 0, 1) . substr($color, 0, 1));
173
            $g = hexdec(substr($color, 1, 1) . substr($color, 1, 1));
174
            $b = hexdec(substr($color, 2, 1) . substr($color, 2, 1));
175
        } elseif ($length == 6) {
176
            $r = hexdec(substr($color, 0, 2));
177
            $g = hexdec(substr($color, 2, 2));
178
            $b = hexdec(substr($color, 4, 2));
179
        }
180
181
        return array($r, $g, $b);
182
    }
183
184
    /**
185
     * @param array<mixed> $rgb1
186
     * @param array<mixed> $rgb2
187
     * 
188
     * @return array<int>
189
     */
190
    private function calculateGradientRgb(array $rgb1, array $rgb2, float $percent): array
191
    {
192
        $r = intval($rgb1[0] + ($rgb2[0] - $rgb1[0]) * $percent / 100);
193
        $g = intval($rgb1[1] + ($rgb2[1] - $rgb1[1]) * $percent / 100);
194
        $b = intval($rgb1[2] + ($rgb2[2] - $rgb1[2]) * $percent / 100);
195
196
        return array($r, $g, $b);
197
    }
198
199
    /**
200
     * @param array<mixed> $rgb
201
     */
202
    private function rgbToHex(array $rgb): string
203
    {
204
        $r = str_pad(dechex((int) $rgb[0]), 2, '0', STR_PAD_LEFT);
205
        $g = str_pad(dechex((int) $rgb[1]), 2, '0', STR_PAD_LEFT);
206
        $b = str_pad(dechex((int) $rgb[2]), 2, '0', STR_PAD_LEFT);
207
208
        return '#' . $r . $g . $b;
209
    }
210
}