Passed
Push — dev ( 02e99e...928e2e )
by Nico
12:03
created

WeaponShield::getModule()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
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\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_waepon_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
    /**
56
     * @ManyToOne(targetEntity="Weapon")
57
     * @JoinColumn(name="weapon_id", referencedColumnName="id")
58
     */
59
    private WeaponInterface $weapon;
60
61
    /**
62
     * @ManyToOne(targetEntity="Module")
63
     * @JoinColumn(name="module_id", referencedColumnName="id")
64
     */
65
    private ModuleInterface $module;
66
67
    public function getId(): int
68
    {
69
        return $this->id;
70
    }
71
72
    public function getModuleId(): int
73
    {
74
        return $this->module_id;
75
    }
76
77
    public function setModuleId(int $moduleId): WeaponShieldInterface
78
    {
79
        $this->module_id = $moduleId;
80
81
        return $this;
82
    }
83
84
    public function getWeaponId(): int
85
    {
86
        return $this->weapon_id;
87
    }
88
89
    public function setWeaponId(int $weaponid): WeaponShieldInterface
90
    {
91
        $this->weapon_id = $weaponid;
92
93
        return $this;
94
    }
95
96
    public function getModificator(): int
97
    {
98
        return $this->modificator;
99
    }
100
101
    public function setModificator(int $Modificator): WeaponShieldInterface
102
    {
103
        $this->modificator = $Modificator;
104
105
        return $this;
106
    }
107
108
    public function getWeapon(): WeaponInterface
109
    {
110
        return $this->weapon;
111
    }
112
113
    public function getModule(): ModuleInterface
114
    {
115
        return $this->module;
116
    }
117
118
    public function calculateGradientColor(): string
119
    {
120
        $color1 = '#00ff00';
121
        $color2 = '#ffd500';
122
        $color3 = '#FF0000';
123
        $percent = 100 / 29 * ($this->getModificator() - 88);
124
125
        // Konvertiere die Hex-Farbcodes in RGB-Werte
126
        $rgb1 = $this->hexToRgb($color1);
127
        $rgb2 = $this->hexToRgb($color2);
128
        $rgb3 = $this->hexToRgb($color3);
129
130
        // Verteile den Prozentwert zwischen den Farben
131
        if ($percent <= 50) {
132
            $gradientPercent = $percent * 2;
133
            $gradientRgb = $this->calculateGradientRgb($rgb1, $rgb2, $gradientPercent);
134
        } else {
135
            $gradientPercent = (($percent - 50) * 2);
136
            $gradientRgb = $this->calculateGradientRgb($rgb2, $rgb3, $gradientPercent);
137
        }
138
139
        // Konvertiere den RGB-Wert zurück in einen Hex-Farbcode
140
        $gradientColor = $this->rgbToHex($gradientRgb);
141
142
        return $gradientColor;
143
    }
144
    /**
145
     * @return array<int, int|float>
146
     */
147
    private function hexToRgb(string $color): array
148
    {
149
        $color = ltrim($color, '#');
150
        $length = strlen($color);
151
        $b = 0;
152
        $g = 0;
153
        $r = 0;
154
        if ($length == 3) {
155
            $r = hexdec(substr($color, 0, 1) . substr($color, 0, 1));
156
            $g = hexdec(substr($color, 1, 1) . substr($color, 1, 1));
157
            $b = hexdec(substr($color, 2, 1) . substr($color, 2, 1));
158
        } elseif ($length == 6) {
159
            $r = hexdec(substr($color, 0, 2));
160
            $g = hexdec(substr($color, 2, 2));
161
            $b = hexdec(substr($color, 4, 2));
162
        }
163
164
        return array($r, $g, $b);
165
    }
166
167
    /**
168
     * @param array<mixed> $rgb1
169
     * @param array<mixed> $rgb2
170
     * 
171
     * @return array<int>
172
     */
173
    private function calculateGradientRgb(array $rgb1, array $rgb2, float $percent): array
174
    {
175
        $r = intval($rgb1[0] + ($rgb2[0] - $rgb1[0]) * $percent / 100);
176
        $g = intval($rgb1[1] + ($rgb2[1] - $rgb1[1]) * $percent / 100);
177
        $b = intval($rgb1[2] + ($rgb2[2] - $rgb1[2]) * $percent / 100);
178
179
        return array($r, $g, $b);
180
    }
181
182
    /**
183
     * @param array<mixed> $rgb
184
     */
185
    private function rgbToHex(array $rgb): string
186
    {
187
        $r = str_pad(dechex((int) $rgb[0]), 2, '0', STR_PAD_LEFT);
188
        $g = str_pad(dechex((int) $rgb[1]), 2, '0', STR_PAD_LEFT);
189
        $b = str_pad(dechex((int) $rgb[2]), 2, '0', STR_PAD_LEFT);
190
191
        return '#' . $r . $g . $b;
192
    }
193
}
194