Alliance::getAvatar()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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\Common\Collections\ArrayCollection;
8
use Doctrine\Common\Collections\Collection;
9
use Doctrine\ORM\Mapping\Column;
10
use Doctrine\ORM\Mapping\Entity;
11
use Doctrine\ORM\Mapping\GeneratedValue;
12
use Doctrine\ORM\Mapping\Id;
13
use Doctrine\ORM\Mapping\JoinColumn;
14
use Doctrine\ORM\Mapping\ManyToOne;
15
use Doctrine\ORM\Mapping\OneToMany;
16
use Doctrine\ORM\Mapping\Table;
17
use Stu\Component\Alliance\Enum\AllianceJobPermissionEnum;
18
use Stu\Component\Alliance\Exception\AllianceFounderNotSetException;
19
use Stu\Orm\Attribute\TruncateOnGameReset;
20
use Stu\Orm\Repository\AllianceRepository;
21
22
#[Table(name: 'stu_alliances')]
23
#[Entity(repositoryClass: AllianceRepository::class)]
24
#[TruncateOnGameReset]
25
class Alliance
26
{
27
    #[Id]
28
    #[Column(type: 'integer')]
29
    #[GeneratedValue(strategy: 'IDENTITY')]
30
    private int $id;
31
32
    #[Column(type: 'string')]
33
    private string $name = '';
34
35
    #[Column(type: 'text')]
36
    private string $description = '';
37
38
    #[Column(type: 'string')]
39
    private string $homepage = '';
40
41
    #[Column(type: 'integer')]
42
    private int $date = 0;
43
44
    #[Column(type: 'integer', nullable: true)]
45
    private ?int $faction_id = null;
0 ignored issues
show
introduced by
The private property $faction_id is not used, and could be removed.
Loading history...
46
47
    #[Column(type: 'boolean')]
48
    private bool $accept_applications = false;
49
50
    #[Column(type: 'string', length: 32)]
51
    private string $avatar = '';
52
53
    #[Column(type: 'string', length: 7)]
54
    private string $rgb_code = '';
55
56
    #[ManyToOne(targetEntity: Faction::class)]
57
    #[JoinColumn(name: 'faction_id', referencedColumnName: 'id')]
58
    private ?Faction $faction = null;
59
60
    /**
61
     * @var ArrayCollection<int, AllianceSettings>
62
     */
63
    #[OneToMany(targetEntity: AllianceSettings::class, mappedBy: 'alliance')]
64
    private Collection $settings;
65
66
    /**
67
     * @var ArrayCollection<int, User>
68
     */
69
    #[OneToMany(targetEntity: User::class, mappedBy: 'alliance')]
70
    private Collection $members;
71
72
    /**
73
     * @var ArrayCollection<int, AllianceJob>
74
     */
75
    #[OneToMany(targetEntity: AllianceJob::class, mappedBy: 'alliance')]
76
    private Collection $jobs;
77
78
    /**
79
     * @var ArrayCollection<int, Station>
80
     */
81
    #[OneToMany(targetEntity: Station::class, mappedBy: 'alliance')]
82
    private Collection $stations;
83
84
    public function __construct()
85
    {
86
        $this->members = new ArrayCollection();
87
        $this->jobs = new ArrayCollection();
88
        $this->settings = new ArrayCollection();
89
        $this->stations = new ArrayCollection();
90
    }
91
92 21
    public function getId(): int
93
    {
94 21
        return $this->id;
95
    }
96
97 11
    public function getName(): string
98
    {
99 11
        return $this->name;
100
    }
101
102
    public function setName(string $name): Alliance
103
    {
104
        $this->name = $name;
105
        return $this;
106
    }
107
108 2
    public function getDescription(): string
109
    {
110 2
        return $this->description;
111
    }
112
113
    public function setDescription(string $description): Alliance
114
    {
115
        $this->description = $description;
116
        return $this;
117
    }
118
119 1
    public function getHomepage(): string
120
    {
121 1
        return $this->homepage;
122
    }
123
124
    public function setHomepage(string $homepage): Alliance
125
    {
126
        $this->homepage = $homepage;
127
        return $this;
128
    }
129
130 1
    public function getDate(): int
131
    {
132 1
        return $this->date;
133
    }
134
135
    public function setDate(int $date): Alliance
136
    {
137
        $this->date = $date;
138
        return $this;
139
    }
140
141 1
    public function getFaction(): ?Faction
142
    {
143 1
        return $this->faction;
144
    }
145
146
    public function setFaction(?Faction $faction): Alliance
147
    {
148
        $this->faction = $faction;
149
        return $this;
150
    }
151
152 2
    public function getAcceptApplications(): bool
153
    {
154 2
        return $this->accept_applications;
155
    }
156
157
    public function setAcceptApplications(bool $acceptApplications): Alliance
158
    {
159
        $this->accept_applications = $acceptApplications;
160
        return $this;
161
    }
162
163
    public function hasAvatar(): bool
164
    {
165
        return strlen($this->getAvatar()) > 0;
166
    }
167
168 1
    public function getAvatar(): string
169
    {
170 1
        return $this->avatar;
171
    }
172
173
    public function setAvatar(string $avatar): Alliance
174
    {
175
        $this->avatar = $avatar;
176
        return $this;
177
    }
178
179 1
    public function getRgbCode(): string
180
    {
181 1
        return $this->rgb_code;
182
    }
183
184
    public function setRgbCode(string $rgbCode): Alliance
185
    {
186
        $this->rgb_code = $rgbCode;
187
        return $this;
188
    }
189
190
    /**
191
     * @throws AllianceFounderNotSetException
192
     */
193
    public function getFounder(): AllianceJob
194
    {
195
        foreach ($this->jobs as $job) {
196
            if ($job->hasPermission(AllianceJobPermissionEnum::FOUNDER->value)) {
197
                return $job;
198
            }
199
        }
200
        throw new AllianceFounderNotSetException();
201
    }
202
203
    public function getSuccessor(): ?AllianceJob
204
    {
205
        foreach ($this->jobs as $job) {
206
            if (
207
                $job->hasPermission(AllianceJobPermissionEnum::SUCCESSOR->value)
208
                && !$job->hasPermission(AllianceJobPermissionEnum::FOUNDER->value)
209
            ) {
210
                return $job;
211
            }
212
        }
213
        return null;
214
    }
215
216
    public function getDiplomatic(): ?AllianceJob
217
    {
218
        foreach ($this->jobs as $job) {
219
            if (
220
                $job->hasPermission(AllianceJobPermissionEnum::DIPLOMATIC->value)
221
                && !$job->hasPermission(AllianceJobPermissionEnum::FOUNDER->value)
222
                && !$job->hasPermission(AllianceJobPermissionEnum::SUCCESSOR->value)
223
            ) {
224
                return $job;
225
            }
226
        }
227
        return null;
228
    }
229
230
    /**
231
     * @return array<AllianceJob>
232
     */
233
    public function getJobsWithFounderPermission(): array
234
    {
235
        return array_values(array_filter(
236
            $this->jobs->toArray(),
237
            fn(AllianceJob $job) => $job->hasPermission(AllianceJobPermissionEnum::FOUNDER->value)
238
        ));
239
    }
240
241
    /**
242
     * @return array<AllianceJob>
243
     */
244
    public function getJobsWithSuccessorPermission(): array
245
    {
246
        return array_values(array_filter(
247
            $this->jobs->toArray(),
248
            fn(AllianceJob $job) => $job->hasPermission(AllianceJobPermissionEnum::SUCCESSOR->value)
249
        ));
250
    }
251
252
    /**
253
     * @return array<AllianceJob>
254
     */
255
    public function getJobsWithDiplomaticPermission(): array
256
    {
257
        return array_values(array_filter(
258
            $this->jobs->toArray(),
259
            fn(AllianceJob $job) => $job->hasPermission(AllianceJobPermissionEnum::DIPLOMATIC->value)
260
        ));
261
    }
262
263
    /**
264
     * @return Collection<int, User>
265
     */
266 2
    public function getMembers(): Collection
267
    {
268 2
        return $this->members;
269
    }
270
271
    public function isNpcAlliance(): bool
272
    {
273
        try {
274
            $founder = $this->getFounder();
275
276
            foreach ($founder->getMemberAssignments() as $assignment) {
277
                if ($assignment->getUser()->isNpc()) {
278
                    return true;
279
                }
280
            }
281
        } catch (AllianceFounderNotSetException) {
282
            return false;
283
        }
284
285
        return false;
286
    }
287
288
    /**
289
     * @return Collection<int, AllianceJob>
290
     */
291 3
    public function getJobs(): Collection
292
    {
293 3
        return $this->jobs;
294
    }
295
296
    public function __toString(): string
297
    {
298
        return $this->getName();
299
    }
300
301 1
    public function hasTranslation(): bool
302
    {
303 1
        $text = $this->getDescription();
304 1
        return strpos($text, '[translate]') !== false && strpos($text, '[/translate]') !== false;
305
    }
306
307
    /**
308
     * @return Collection<int, AllianceSettings>
309
     */
310 1
    public function getSettings(): Collection
311
    {
312 1
        return $this->settings;
313
    }
314
315
    /**
316
     * @return Collection<int, Station>
317
     */
318
    public function getStations(): Collection
319
    {
320
        return $this->stations;
321
    }
322
}
323