TeamRequest::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VideoGamesRecords\CoreBundle\Entity;
6
7
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
8
use ApiPlatform\Metadata\ApiFilter;
9
use ApiPlatform\Metadata\ApiResource;
10
use ApiPlatform\Metadata\Get;
11
use ApiPlatform\Metadata\GetCollection;
12
use ApiPlatform\Metadata\Link;
13
use ApiPlatform\Metadata\Post;
14
use ApiPlatform\Metadata\Put;
15
use Doctrine\ORM\Mapping as ORM;
16
use Gedmo\Timestampable\Traits\TimestampableEntity;
17
use Symfony\Component\Validator\Constraints as Assert;
18
use VideoGamesRecords\CoreBundle\Repository\TeamRequestRepository;
19
use VideoGamesRecords\CoreBundle\ValueObject\TeamRequestStatus;
20
21
#[ORM\Table(name:'vgr_team_request')]
22
#[ORM\Entity(repositoryClass: TeamRequestRepository::class)]
23
#[ORM\EntityListeners(["VideoGamesRecords\CoreBundle\EventListener\Entity\TeamRequestListener"])]
24
#[ApiResource(
25
    operations: [
26
        new GetCollection(),
27
        new Get(),
28
        new Post(
29
            denormalizationContext: ['groups' => ['team-request:insert']],
30
            security: 'is_granted("ROLE_PLAYER")'
31
        ),
32
        new Put(
33
            denormalizationContext: ['groups' => ['team-request:update']],
34
            security: 'is_granted("ROLE_PLAYER") and ((object.getTeam().getLeader().getUserId() == user.getId()) or (object.getPlayer().getUserId() == user.getId()))'
35
        ),
36
    ],
37
    normalizationContext: ['groups' => [
38
        'team-request:read', 'team-request:player', 'player:read-minimal', 'team-request:team']
39
    ],
40
)]
41
#[ApiResource(
42
    uriTemplate: '/players/{id}/team_requests',
43
    uriVariables: [
44
        'id' => new Link(fromClass: Player::class, toProperty: 'player'),
45
    ],
46
    operations: [ new GetCollection() ],
47
    normalizationContext: ['groups' => [
48
        'team-request:read', 'team-request:team', 'team:read:minimal',]
49
    ],
50
)]
51
#[ApiResource(
52
    uriTemplate: '/teams/{id}/team_requests',
53
    uriVariables: [
54
        'id' => new Link(fromClass: Team::class, toProperty: 'team'),
55
    ],
56
    operations: [ new GetCollection() ],
57
    normalizationContext: ['groups' => [
58
        'team-request:read', 'team-request:player', 'player:read:minimal',]
59
    ],
60
)]
61
#[ApiFilter(
62
    SearchFilter::class,
63
    properties: [
64
        'status' => 'exact',
65
    ]
66
)]
67
class TeamRequest
68
{
69
    use TimestampableEntity;
70
71
    #[ORM\Id, ORM\Column, ORM\GeneratedValue]
72
    private ?int $id = null;
73
74
    #[Assert\Length(max: 30)]
75
    #[ORM\Column(length: 30, nullable: false)]
76
    private string $status = TeamRequestStatus::ACTIVE;
77
78
    #[ORM\ManyToOne(targetEntity: Team::class)]
79
    #[ORM\JoinColumn(name:'team_id', referencedColumnName:'id', nullable:false, onDelete: 'CASCADE')]
80
    private Team $team;
81
82
    #[ORM\ManyToOne(targetEntity: Player::class)]
83
    #[ORM\JoinColumn(name:'player_id', referencedColumnName:'id', nullable:false, onDelete: 'CASCADE')]
84
    private Player $player;
85
86
    public function __toString()
87
    {
88
        return sprintf('%s # %s [%s]', $this->getTeam()->getLibTeam(), $this->getPlayer()->getPseudo(), $this->id);
89
    }
90
91
    public function setId(int $id): void
92
    {
93
        $this->id = $id;
94
    }
95
96
    public function getId(): ?int
97
    {
98
        return $this->id;
99
    }
100
101
    public function setStatus(string $status): void
102
    {
103
        $value = new TeamRequestStatus($status);
104
        $this->status = $value->getValue();
105
    }
106
107
    public function getStatus(): string
108
    {
109
        return $this->status;
110
    }
111
112
    public function getTeamRequestStatus(): TeamRequestStatus
113
    {
114
        return new TeamRequestStatus($this->status);
115
    }
116
117
    public function setPlayer(Player $player): void
118
    {
119
        $this->player = $player;
120
    }
121
122
    public function getPlayer(): Player
123
    {
124
        return $this->player;
125
    }
126
127
    public function setTeam(Team $team): void
128
    {
129
        $this->team = $team;
130
    }
131
132
    public function getTeam(): Team
133
    {
134
        return $this->team;
135
    }
136
}
137