Failed Conditions
Pull Request — master (#2358)
by Nico
10:36 queued 05:05
created

AllianceJob::getId()   A

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\Orm\Attribute\TruncateOnGameReset;
18
use Stu\Orm\Repository\AllianceJobRepository;
19
20
#[Table(name: 'stu_alliances_jobs')]
21
#[Entity(repositoryClass: AllianceJobRepository::class)]
22
#[TruncateOnGameReset]
23
class AllianceJob
24
{
25
    #[Id]
26
    #[Column(type: 'integer')]
27
    #[GeneratedValue(strategy: 'IDENTITY')]
28
    private int $id;
29
30
    #[ManyToOne(targetEntity: Alliance::class, inversedBy: 'jobs')]
31
    #[JoinColumn(name: 'alliance_id', nullable: false, referencedColumnName: 'id', onDelete: 'CASCADE')]
32
    private Alliance $alliance;
33
34
    #[Column(type: 'string', nullable: true)]
35
    private ?string $title = null;
36
37
    #[Column(type: 'integer', nullable: true)]
38
    private ?int $sort = null;
39
40
    #[Column(type: 'boolean', options: ['default' => false])]
41
    private bool $is_founder_permission = false;
0 ignored issues
show
introduced by
The private property $is_founder_permission is not used, and could be removed.
Loading history...
42
43
    #[Column(type: 'boolean', options: ['default' => false])]
44
    private bool $is_successor_permission = false;
0 ignored issues
show
introduced by
The private property $is_successor_permission is not used, and could be removed.
Loading history...
45
46
    #[Column(type: 'boolean', options: ['default' => false])]
47
    private bool $is_diplomatic_permission = false;
0 ignored issues
show
introduced by
The private property $is_diplomatic_permission is not used, and could be removed.
Loading history...
48
49
    /**
50
     * @var ArrayCollection<int, AllianceMemberJob>
51
     */
52
    #[OneToMany(targetEntity: AllianceMemberJob::class, mappedBy: 'job', cascade: ['remove'])]
53
    private Collection $memberAssignments;
54
55
    /**
56
     * @var ArrayCollection<int, AllianceJobPermission>
57
     */
58
    #[OneToMany(targetEntity: AllianceJobPermission::class, mappedBy: 'job', cascade: ['persist', 'remove'])]
59
    private Collection $permissions;
60
61
    public function __construct()
62
    {
63
        $this->memberAssignments = new ArrayCollection();
64
        $this->permissions = new ArrayCollection();
65
    }
66
67 1
    public function getId(): int
68
    {
69 1
        return $this->id;
70
    }
71
72 9
    public function getAlliance(): Alliance
73
    {
74 9
        return $this->alliance;
75
    }
76
77
    public function setAlliance(Alliance $alliance): AllianceJob
78
    {
79
        $this->alliance = $alliance;
80
        return $this;
81
    }
82
83 2
    public function getTitle(): ?string
84
    {
85 2
        return $this->title;
86
    }
87
88
    public function setTitle(string $title): AllianceJob
89
    {
90
        $this->title = $title;
91
        return $this;
92
    }
93
94 3
    public function getSort(): ?int
95
    {
96 3
        return $this->sort;
97
    }
98
99
    public function setSort(?int $sort): AllianceJob
100
    {
101
        $this->sort = $sort;
102
        return $this;
103
    }
104
105
    /**
106
     * @return Collection<int, AllianceMemberJob>
107
     */
108
    public function getMemberAssignments(): Collection
109
    {
110
        return $this->memberAssignments;
111
    }
112
113
    /**
114
     * @return Collection<int, AllianceJobPermission>
115
     */
116
    public function getPermissions(): Collection
117
    {
118
        return $this->permissions;
119
    }
120
121 9
    public function hasPermission(int $permissionType): bool
122
    {
123 9
        foreach ($this->permissions as $permission) {
124 9
            if ($permission->getPermission() === $permissionType) {
125 9
                return true;
126
            }
127
        }
128 2
        return false;
129
    }
130
131
    /**
132
     * @return array<User>
133
     */
134 1
    public function getUsers(): array
135
    {
136 1
        return array_map(
137 1
            fn(AllianceMemberJob $assignment) => $assignment->getUser(),
138 1
            $this->memberAssignments->toArray()
139 1
        );
140
    }
141
142 2
    public function hasUser(User $user): bool
143
    {
144 2
        foreach ($this->memberAssignments as $assignment) {
145 2
            if ($assignment->getUser()->getId() === $user->getId()) {
146 2
                return true;
147
            }
148
        }
149 2
        return false;
150
    }
151
152 1
    public function getUser(): ?User
153
    {
154 1
        $first = $this->memberAssignments->first();
155 1
        return $first !== false ? $first->getUser() : null;
156
    }
157
158 1
    public function getUserId(): ?int
159
    {
160 1
        $user = $this->getUser();
161 1
        return $user !== null ? $user->getId() : null;
162
    }
163
}
164