Passed
Push — dev ( f9892a...9a2fe6 )
by Nico
37:15 queued 29:10
created

WeaponShield::getFactionByModule()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

217
            /** @scrutinizer ignore-call */ 
218
            $result = $this->findBy([

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
218
                'faction_id' => $index,
219
                'module_id' => $moduleid
220
            ]);
221
222
            if ($result) {
223
                $results->add($result);
224
            }
225
        }
226
227
        return $results;
228
    }
229
}