Passed
Push — master ( ad0a87...920d42 )
by Nico
05:24
created

Alliance::setFaction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 3
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\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\AllianceJobTypeEnum;
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
    /**
68
     * @var ArrayCollection<int, User>
69
     */
70
    #[OneToMany(targetEntity: User::class, mappedBy: 'alliance')]
71
    private Collection $members;
72
73
    /**
74
     * @var ArrayCollection<int, AllianceJob>
75
     */
76
    #[OneToMany(targetEntity: AllianceJob::class, mappedBy: 'alliance')]
77
    private Collection $jobs;
78
79
80
    public function __construct()
81
    {
82
        $this->members = new ArrayCollection();
83
        $this->jobs = new ArrayCollection();
84
        $this->settings = new ArrayCollection();
85
    }
86
87 21
    public function getId(): int
88
    {
89 21
        return $this->id;
90
    }
91
92 9
    public function getName(): string
93
    {
94 9
        return $this->name;
95
    }
96
97
    public function setName(string $name): Alliance
98
    {
99
        $this->name = $name;
100
        return $this;
101
    }
102
103 2
    public function getDescription(): string
104
    {
105 2
        return $this->description;
106
    }
107
108
    public function setDescription(string $description): Alliance
109
    {
110
        $this->description = $description;
111
        return $this;
112
    }
113
114 1
    public function getHomepage(): string
115
    {
116 1
        return $this->homepage;
117
    }
118
119
    public function setHomepage(string $homepage): Alliance
120
    {
121
        $this->homepage = $homepage;
122
        return $this;
123
    }
124
125 1
    public function getDate(): int
126
    {
127 1
        return $this->date;
128
    }
129
130
    public function setDate(int $date): Alliance
131
    {
132
        $this->date = $date;
133
        return $this;
134
    }
135
136 1
    public function getFaction(): ?Faction
137
    {
138 1
        return $this->faction;
139
    }
140
141
    public function setFaction(?Faction $faction): Alliance
142
    {
143
        $this->faction = $faction;
144
        return $this;
145
    }
146
147 2
    public function getAcceptApplications(): bool
148
    {
149 2
        return $this->accept_applications;
150
    }
151
152
    public function setAcceptApplications(bool $acceptApplications): Alliance
153
    {
154
        $this->accept_applications = $acceptApplications;
155
        return $this;
156
    }
157
158
    public function hasAvatar(): bool
159
    {
160
        return strlen($this->getAvatar()) > 0;
161
    }
162
163 1
    public function getAvatar(): string
164
    {
165 1
        return $this->avatar;
166
    }
167
168
    public function setAvatar(string $avatar): Alliance
169
    {
170
        $this->avatar = $avatar;
171
        return $this;
172
    }
173
174 1
    public function getRgbCode(): string
175
    {
176 1
        return $this->rgb_code;
177
    }
178
179
    public function setRgbCode(string $rgbCode): Alliance
180
    {
181
        $this->rgb_code = $rgbCode;
182
        return $this;
183
    }
184
185
    /**
186
     * @throws AllianceFounderNotSetException
187
     */
188
    public function getFounder(): AllianceJob
189
    {
190
        foreach ($this->jobs as $job) {
191
            if ($job->hasFounderPermission()) {
192
                return $job;
193
            }
194
        }
195
        throw new AllianceFounderNotSetException();
196
    }
197
198
    public function getSuccessor(): ?AllianceJob
199
    {
200
        foreach ($this->jobs as $job) {
201
            if ($job->hasSuccessorPermission() && !$job->hasFounderPermission()) {
202
                return $job;
203
            }
204
        }
205
        return null;
206
    }
207
208
    public function getDiplomatic(): ?AllianceJob
209
    {
210
        foreach ($this->jobs as $job) {
211
            if ($job->hasDiplomaticPermission() && !$job->hasFounderPermission() && !$job->hasSuccessorPermission()) {
212
                return $job;
213
            }
214
        }
215
        return null;
216
    }
217
218
    /**
219
     * @return array<AllianceJob>
220
     */
221 1
    public function getJobsWithFounderPermission(): array
222
    {
223 1
        return array_values(array_filter(
224 1
            $this->jobs->toArray(),
225 1
            fn(AllianceJob $job) => $job->hasFounderPermission()
226 1
        ));
227
    }
228
229
    /**
230
     * @return array<AllianceJob>
231
     */
232
    public function getJobsWithSuccessorPermission(): array
233
    {
234
        return array_values(array_filter(
235
            $this->jobs->toArray(),
236
            fn(AllianceJob $job) => $job->hasSuccessorPermission()
237
        ));
238
    }
239
240
    /**
241
     * @return array<AllianceJob>
242
     */
243
    public function getJobsWithDiplomaticPermission(): array
244
    {
245
        return array_values(array_filter(
246
            $this->jobs->toArray(),
247
            fn(AllianceJob $job) => $job->hasDiplomaticPermission()
248
        ));
249
    }
250
251
    /**
252
     * @return Collection<int, User>
253
     */
254 2
    public function getMembers(): Collection
255
    {
256 2
        return $this->members;
257
    }
258
259
    /**
260
     * Returns `true` if the founder is a npc
261
     */
262
    public function isNpcAlliance(): bool
263
    {
264
        try {
265
            $founder = $this->getFounder();
266
267
            foreach ($founder->getMemberAssignments() as $assignment) {
268
                if ($assignment->getUser()->isNpc()) {
269
                    return true;
270
                }
271
            }
272
        } catch (AllianceFounderNotSetException) {
273
            return false;
274
        }
275
276
        return false;
277
    }
278
279
    /**
280
     * Returns the alliance jobs, indexed by type
281
     *
282
     * @return Collection<int, AllianceJob>
283
     */
284 3
    public function getJobs(): Collection
285
    {
286 3
        return $this->jobs;
287
    }
288
289
    public function __toString(): string
290
    {
291
        return $this->getName();
292
    }
293
294 1
    public function hasTranslation(): bool
295
    {
296 1
        $text = $this->getDescription();
297 1
        return strpos($text, '[translate]') !== false && strpos($text, '[/translate]') !== false;
298
    }
299
300
    /**
301
     * @return Collection<int, AllianceSettings>
302
     */
303 1
    public function getSettings(): Collection
304
    {
305 1
        return $this->settings;
306
    }
307
}
308