Member   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 179
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 78.79%

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 4
dl 0
loc 179
ccs 26
cts 33
cp 0.7879
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A getUser() 0 4 1
A getName() 0 4 1
A setName() 0 6 1
A getPoints() 0 4 1
A setPoints() 0 6 1
A addPoints() 0 6 1
A getRole() 0 4 1
A setRole() 0 6 1
A getMemberships() 0 4 1
A getMemberNeeds() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the zibios/sharep.
7
 *
8
 * (c) Zbigniew Ślązak
9
 */
10
11
namespace App\Entity\Parking;
12
13
use ApiPlatform\Core\Annotation as OA;
14
use App\Entity\Access\User;
15
use App\Entity\EntityInterface;
16
use App\Entity\GetRoleInterface;
17
use App\Entity\Traits;
18
use App\Enum\Functional\RoleEnum;
19
use Doctrine\Common\Collections\ArrayCollection;
20
use Doctrine\ORM\Mapping as ORM;
21
use PascalDeVink\ShortUuid\ShortUuid;
22
use Symfony\Bridge\Doctrine\Validator\Constraints as OrmAssert;
23
use Symfony\Component\Serializer\Annotation as Serializer;
24
use Symfony\Component\Validator\Constraints as Assert;
25
26
/**
27
 * @ORM\Entity(repositoryClass="App\Repository\Entity\Parking\MemberRepository")
28
 * @ORM\Table(name="parking_members")
29
 * @OrmAssert\UniqueEntity(fields={"name"}, errorPath="name", message="Name already exist")
30
 * @OrmAssert\UniqueEntity(fields={"user"}, errorPath="user", message="User already exist")
31
 * @OA\ApiResource(
32
 *   input={"class"="\App\Api\Dto\Parking\MemberInputDto"},
33
 *   normalizationContext={"groups"={"get"}},
34
 *   itemOperations={
35
 *     "get"={
36
 *       "access_control"="is_granted('PARKING_MEMBER_READ')"
37
 *     },
38
 *     "delete"={
39
 *       "controller"="\App\Controller\Api\Parking\MemberDeleteAction",
40
 *       "access_control"="is_granted('PARKING_MEMBER_DELETE', object)"
41
 *     },
42
 *     "put"={
43
 *       "denormalization_context"={"groups"={"put"}},
44
 *       "access_control"="is_granted('PARKING_MEMBER_UPDATE', object) and is_granted('PARKING_MEMBER_UPDATE', previous_object)"
45
 *     }
46
 *   },
47
 *   collectionOperations={
48
 *     "get"={
49
 *       "access_control"="is_granted('PARKING_MEMBER_READ')"
50
 *     },
51
 *     "post"={
52
 *       "denormalization_context"={"groups"={"post"}},
53
 *       "access_control"="is_granted('PARKING_MEMBER_CREATE', object)"
54
 *     }
55
 *   }
56
 * )
57
 */
58
class Member implements EntityInterface, GetRoleInterface
59
{
60
    use Traits\PropertyIdGeneratedTrait;
61
62
    /**
63
     * @var string
64
     * @ORM\Column(name="name", type="string", length=255, nullable=false, unique=true)
65
     *
66
     * @Assert\NotBlank()
67
     * @Assert\Length(min="1", max="255")
68
     *
69
     * @Serializer\Groups({"get"})
70
     * @OA\ApiProperty(
71
     *     attributes={
72
     *         "swagger_context"={"type"="string", "example"="John Doe"},
73
     *     }
74
     * )
75
     * @OA\ApiFilter(
76
     *     ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter::class,
77
     *     strategy="partial"
78
     * )
79
     */
80
    private $name;
81
82
    /**
83
     * @var int
84
     * @ORM\Column(name="points", type="integer", nullable=false)
85
     *
86
     * @Assert\PositiveOrZero()
87
     *
88
     * @Serializer\Groups({"get"})
89
     * @OA\ApiProperty(
90
     *     attributes={
91
     *         "swagger_context"={"type"="integer", "example"="0"},
92
     *     }
93
     * )
94
     * @OA\ApiFilter(
95
     *     ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\RangeFilter::class
96
     * )
97
     * @OA\ApiFilter(
98
     *     ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter::class,
99
     *     strategy="exact"
100
     * )
101
     */
102
    private $points;
103
104
    /**
105
     * @var string
106
     * @ORM\Column(name="role", type="string", length=50, nullable=false)
107
     *
108
     * @Assert\NotBlank()
109
     * @Assert\Choice(callback={"App\Enum\Functional\RoleEnum", "toArray"})
110
     * @Assert\Length(min="1", max="50")
111
     *
112
     * @Serializer\Groups({"get"})
113
     * @OA\ApiProperty(
114
     *     attributes={
115
     *         "swagger_context"={"type"="string", "enum"={"ROLE_MEMBER", "ROLE_MANAGER"}, "example"="ROLE_MEMBER"},
116
     *     }
117
     * )
118
     * @OA\ApiFilter(
119
     *     ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter::class,
120
     *     strategy="exact"
121
     * )
122
     */
123
    private $role;
124
125
    //-------------------------------------------------------------------------------------------
126
127
    /**
128
     * @var User|null
129
     * @ORM\OneToOne(targetEntity="App\Entity\Access\User", inversedBy="member", cascade={"persist"})
130
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=true, unique=true)
131
     */
132
    private $user;
133
134
    /**
135
     * @var ArrayCollection|Membership[]
136
     * @ORM\OneToMany(targetEntity="App\Entity\Parking\Membership", mappedBy="member")
137
     */
138
    private $memberships;
139
140
    /**
141
     * @var ArrayCollection|MemberNeed[]
142
     * @ORM\OneToMany(targetEntity="App\Entity\Parking\MemberNeed", mappedBy="member")
143
     *
144
     * @OA\ApiSubresource(maxDepth=1)
145
     * @OA\ApiFilter(
146
     *     ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\DateFilter::class,
147
     *     strategy="ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\DateFilter::INCLUDE_NULL_BEFORE",
148
     *     properties={"memberNeeds.date"}
149
     * )
150
     * @OA\ApiFilter(
151
     *     ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter::class,
152
     *     properties={"memberNeeds.date"},
153
     *     strategy="exact"
154
     * )
155
     */
156
    private $memberNeeds;
157
158
    //-------------------------------------------------------------------------------------------
159
160 14
    public function __construct(string $name, RoleEnum $role, ?User $user)
161
    {
162 14
        $this->id = ShortUuid::uuid4();
163 14
        $this->user = $user;
164 14
        $this->setName($name);
165 14
        $this->setPoints(0);
166 14
        $this->setRole($role);
167 14
        $this->memberships = new ArrayCollection();
168 14
        $this->memberNeeds = new ArrayCollection();
169 14
    }
170
171
    //-------------------------------------------------------------------------------------------
172
173 3
    public function getUser(): ?User
174
    {
175 3
        return $this->user;
176
    }
177
178 8
    public function getName(): string
179
    {
180 8
        return $this->name;
181
    }
182
183 15
    public function setName(string $name): self
184
    {
185 15
        $this->name = trim($name);
186
187 15
        return $this;
188
    }
189
190 8
    public function getPoints(): int
191
    {
192 8
        return $this->points;
193
    }
194
195 14
    public function setPoints(int $points): self
196
    {
197 14
        $this->points = $points;
198
199 14
        return $this;
200
    }
201
202
    public function addPoints(int $points): self
203
    {
204
        $this->points += $points;
205
206
        return $this;
207
    }
208
209 16
    public function getRole(): RoleEnum
210
    {
211 16
        return new RoleEnum($this->role);
212
    }
213
214 15
    public function setRole(RoleEnum $role): self
215
    {
216 15
        $this->role = $role->getValue();
217
218 15
        return $this;
219
    }
220
221
    /**
222
     * @return Membership[]|ArrayCollection
223
     */
224
    public function getMemberships(): ArrayCollection
225
    {
226
        return $this->memberships;
227
    }
228
229
    /**
230
     * @return MemberNeed[]|ArrayCollection
231
     */
232
    public function getMemberNeeds(): ArrayCollection
233
    {
234
        return $this->memberNeeds;
235
    }
236
}
237