Passed
Pull Request — master (#6838)
by
unknown
08:40
created

CBlogPost::getAuthorInfo()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 9
nop 0
dl 0
loc 13
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CourseBundle\Entity;
8
9
use ApiPlatform\Metadata\ApiResource;
10
use ApiPlatform\Metadata\Delete;
11
use ApiPlatform\Metadata\Get;
12
use ApiPlatform\Metadata\GetCollection;
13
use ApiPlatform\Metadata\Patch;
14
use ApiPlatform\Metadata\Post;
15
use Chamilo\CoreBundle\Entity\User;
16
use Chamilo\CoreBundle\State\CBlogAssignAuthorProcessor;
17
use Chamilo\CourseBundle\Repository\CBlogPostRepository;
18
use DateTime;
19
use Doctrine\Common\Collections\ArrayCollection;
20
use Doctrine\Common\Collections\Collection;
21
use Doctrine\ORM\Mapping as ORM;
22
use Symfony\Component\Serializer\Annotation\Groups;
23
24
#[ApiResource(
25
    operations: [
26
        new Get(),
27
        new GetCollection(),
28
        new Post(
29
            security: "is_granted('ROLE_USER')",
30
            processor: CBlogAssignAuthorProcessor::class
31
        ),
32
        new Patch(security: "is_granted('ROLE_CURRENT_COURSE_TEACHER') or is_granted('ROLE_TEACHER') or (object.getAuthor() != null and object.getAuthor() === user)"),
33
        new Delete(security: "is_granted('ROLE_CURRENT_COURSE_TEACHER') or is_granted('ROLE_TEACHER') or (object.getAuthor() != null and object.getAuthor() === user)"),
34
    ],
35
    normalizationContext: ['groups' => ['blog_post:read']],
36
    denormalizationContext: ['groups' => ['blog_post:write']],
37
    paginationEnabled: true
38
)]
39
#[ORM\Entity(repositoryClass: CBlogPostRepository::class)]
40
#[ORM\Table(name: 'c_blog_post')]
41
#[ORM\HasLifecycleCallbacks]
42
class CBlogPost
43
{
44
    #[Groups(['blog_post:read'])]
45
    #[ORM\Id, ORM\Column(type: 'integer'), ORM\GeneratedValue]
46
    protected ?int $iid = null;
47
48
    #[Groups(['blog_post:read','blog_post:write'])]
49
    #[ORM\Column(type: 'string', length: 250)]
50
    protected string $title;
51
52
    #[Groups(['blog_post:read','blog_post:write'])]
53
    #[ORM\Column(name: 'full_text', type: 'text')]
54
    protected string $fullText;
55
56
    #[Groups(['blog_post:read'])]
57
    #[ORM\Column(name: 'date_creation', type: 'datetime')]
58
    protected DateTime $dateCreation;
59
60
    #[Groups(['blog_post:read'])]
61
    #[ORM\ManyToOne(targetEntity: User::class)]
62
    #[ORM\JoinColumn(name: 'author_id', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')]
63
    protected ?User $author = null;
64
65
    #[Groups(['blog_post:read','blog_post:write'])]
66
    #[ORM\ManyToOne(targetEntity: CBlog::class)]
67
    #[ORM\JoinColumn(name: 'blog_id', referencedColumnName: 'iid', onDelete: 'CASCADE')]
68
    protected ?CBlog $blog = null;
69
70
    #[Groups(['blog_post:read'])]
71
    #[ORM\OneToMany(mappedBy: 'post', targetEntity: CBlogAttachment::class, cascade: ['persist', 'remove'])]
72
    protected Collection $attachments;
73
74
    public function __construct()
75
    {
76
        $this->attachments = new ArrayCollection();
77
    }
78
79
    #[ORM\PrePersist]
80
    public function prePersistSetDate(): void
81
    {
82
        if (!isset($this->dateCreation)) {
83
            $this->dateCreation = new DateTime();
84
        }
85
    }
86
87
    public function getIid(): ?int
88
    {
89
        return $this->iid;
90
    }
91
92
    public function getTitle(): string
93
    {
94
        return $this->title;
95
    }
96
97
    public function setTitle(string $title): self
98
    {
99
        $this->title = $title;
100
101
        return $this;
102
    }
103
104
    public function getFullText(): string
105
    {
106
        return $this->fullText;
107
    }
108
109
    public function setFullText(string $fullText): self
110
    {
111
        $this->fullText = $fullText;
112
113
        return $this;
114
    }
115
116
    public function getDateCreation(): DateTime
117
    {
118
        return $this->dateCreation;
119
    }
120
121
    public function setDateCreation(DateTime $dateCreation): self
122
    {
123
        $this->dateCreation = $dateCreation;
124
125
        return $this;
126
    }
127
128
    public function getAuthor(): ?User
129
    {
130
        return $this->author ?? null;
131
    }
132
133
    public function setAuthor(?User $author): self
134
    {
135
        $this->author = $author;
136
        return $this;
137
    }
138
139
    public function getBlog(): ?CBlog
140
    {
141
        return $this->blog;
142
    }
143
144
    public function setBlog(?CBlog $blog): self
145
    {
146
        $this->blog = $blog;
147
148
        return $this;
149
    }
150
151
    /** @return Collection<int, CBlogAttachment> */
152
    public function getAttachments(): Collection
153
    {
154
        return $this->attachments;
155
    }
156
157
    public function addAttachment(CBlogAttachment $attachment): self
158
    {
159
        if (!$this->attachments->contains($attachment)) {
160
            $this->attachments->add($attachment);
161
            $attachment->setPost($this);
162
            if ($this->blog && $attachment->getBlog() !== $this->blog) {
163
                $attachment->setBlog($this->blog);
164
            }
165
        }
166
167
        return $this;
168
    }
169
170
    public function removeAttachment(CBlogAttachment $attachment): self
171
    {
172
        if ($this->attachments->removeElement($attachment)) {
173
            if ($attachment->getPost() === $this) {
174
                $attachment->setPost(null);
175
            }
176
        }
177
178
        return $this;
179
    }
180
181
    #[Groups(['blog_post:read'])]
182
    public function getAuthorInfo(): array
183
    {
184
        $u = $this->getAuthor();
185
        if (!$u) {
186
            return ['id' => null, 'name' => '—'];
187
        }
188
        $name = method_exists($u, 'getFullName') ? $u->getFullName()
189
            : (method_exists($u, 'getUsername') ? $u->getUsername() : 'User');
190
191
        return [
192
            'id'   => method_exists($u, 'getId') ? $u->getId() : null,
193
            'name' => $name,
194
        ];
195
    }
196
}
197