Passed
Push — master ( e9e739...1e7f19 )
by Yannick
09:01
created

CBlog::getOwner()   A

Complexity

Conditions 6
Paths 9

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 9
nc 9
nop 0
dl 0
loc 15
rs 9.2222
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\Doctrine\Orm\Filter\OrderFilter;
10
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
11
use ApiPlatform\Metadata\ApiFilter;
12
use ApiPlatform\Metadata\ApiResource;
13
use ApiPlatform\Metadata\Delete;
14
use ApiPlatform\Metadata\Get;
15
use ApiPlatform\Metadata\GetCollection;
16
use ApiPlatform\Metadata\Patch;
17
use ApiPlatform\Metadata\Post;
18
use ApiPlatform\Metadata\Put;
19
use Chamilo\CoreBundle\Controller\Api\CreateCBlogAction;
20
use Chamilo\CoreBundle\Controller\Api\UpdateVisibilityBlog;
21
use Chamilo\CoreBundle\Entity\AbstractResource;
22
use Chamilo\CoreBundle\Entity\ResourceLink;
23
use Chamilo\CoreBundle\Entity\ResourceInterface;
24
use Chamilo\CourseBundle\Repository\CBlogRepository;
25
use DateTime;
26
use Doctrine\Common\Collections\ArrayCollection;
27
use Doctrine\Common\Collections\Collection;
28
use Doctrine\ORM\Mapping as ORM;
29
use Stringable;
30
use Symfony\Component\Serializer\Annotation\Groups;
31
use Symfony\Component\Validator\Constraints as Assert;
32
33
#[ORM\Entity(repositoryClass: CBlogRepository::class)]
34
#[ORM\Table(name: 'c_blog')]
35
#[ApiResource(
36
    operations: [
37
        new GetCollection(),
38
        new Post(
39
            controller: CreateCBlogAction::class,
40
            openapiContext: [
41
                'summary' => 'Create a new blog project',
42
                'requestBody' => [
43
                    'content' => [
44
                        'application/json' => [
45
                            'schema' => [
46
                                'type' => 'object',
47
                                'properties' => [
48
                                    'title' => ['type' => 'string'],
49
                                    'blogSubtitle' => ['type' => 'string'],
50
                                    'parentResourceNodeId' => ['type' => 'integer'],
51
                                    'resourceLinkList' => [
52
                                        'oneOf' => [
53
                                            ['type' => 'array'],
54
                                            ['type' => 'string'],
55
                                        ],
56
                                    ],
57
                                    'showOnHomepage' => ['type' => 'boolean'],
58
                                ],
59
                                'required' => ['title'],
60
                            ],
61
                        ],
62
                    ],
63
                ],
64
            ],
65
            security: "is_granted('ROLE_CURRENT_COURSE_TEACHER') or is_granted('ROLE_CURRENT_COURSE_SESSION_TEACHER') or is_granted('ROLE_TEACHER')",
66
            validationContext: ['groups' => ['Default','blog:write']],
67
            deserialize: false
68
        ),
69
        new Get(security: "is_granted('VIEW', object.resourceNode)"),
70
        new Patch(security: "is_granted('ROLE_USER')"),
71
        new Delete(security: "is_granted('ROLE_USER')"),
72
        new Put(
73
            uriTemplate: '/c_blogs/{iid}/toggle_visibility',
74
            controller: UpdateVisibilityBlog::class,
75
            security: "is_granted('EDIT', object.resourceNode)",
76
            deserialize: false
77
        ),
78
    ],
79
    normalizationContext: ['groups' => ['blog:read']],
80
    denormalizationContext: ['groups' => ['blog:write', 'resource_node:write']],
81
    paginationEnabled: true
82
)]
83
#[ApiFilter(SearchFilter::class, properties: [
84
    'title' => 'partial',
85
    'blogSubtitle' => 'partial',
86
])]
87
#[ApiFilter(OrderFilter::class, properties: [
88
    'dateCreation',
89
    'title',
90
], arguments: ['orderParameterName' => 'order'])]
91
class CBlog extends AbstractResource implements ResourceInterface, Stringable
92
{
93
    #[Groups(['blog:read'])]
94
    #[ORM\Id, ORM\Column(name: 'iid', type: 'integer'), ORM\GeneratedValue]
95
    protected ?int $iid = null;
96
97
    #[Assert\NotBlank]
98
    #[Groups(['blog:read','blog:write'])]
99
    #[ORM\Column(name: 'title', type: 'text', nullable: false)]
100
    protected string $title;
101
102
    #[Groups(['blog:read','blog:write'])]
103
    #[ORM\Column(name: 'blog_subtitle', type: 'string', length: 250, nullable: true)]
104
    protected ?string $blogSubtitle = null;
105
106
    #[Groups(['blog:read'])]
107
    #[ORM\Column(name: 'date_creation', type: 'datetime', nullable: false)]
108
    protected DateTime $dateCreation;
109
110
    #[Groups(['blog:read'])]
111
    #[ORM\OneToMany(mappedBy: 'blog', targetEntity: CBlogAttachment::class, cascade: ['persist', 'remove'])]
112
    private Collection $attachments;
113
114
    public function __construct()
115
    {
116
        $this->attachments = new ArrayCollection();
117
        $this->dateCreation = new DateTime();
118
    }
119
120
    public function getIid(): ?int { return $this->iid; }
121
122
    public function getTitle(): string { return $this->title; }
123
    public function setTitle(string $title): self { $this->title = $title; return $this; }
124
125
    public function getBlogSubtitle(): ?string { return $this->blogSubtitle; }
126
    public function setBlogSubtitle(?string $blogSubtitle): self { $this->blogSubtitle = $blogSubtitle; return $this; }
127
128
    public function getDateCreation(): DateTime { return $this->dateCreation; }
129
    public function setDateCreation(DateTime $dateCreation): self { $this->dateCreation = $dateCreation; return $this; }
130
131
    /** @return Collection<int, CBlogAttachment> */
132
    public function getAttachments(): Collection { return $this->attachments; }
133
    public function addAttachment(CBlogAttachment $attachment): self
134
    {
135
        if (!$this->attachments->contains($attachment)) {
136
            $this->attachments->add($attachment);
137
            $attachment->setBlog($this);
138
        }
139
        return $this;
140
    }
141
    public function removeAttachment(CBlogAttachment $attachment): self
142
    {
143
        if ($this->attachments->removeElement($attachment)) {
144
            if ($attachment->getBlog() === $this) {
145
                $attachment->setBlog(null);
146
            }
147
        }
148
        return $this;
149
    }
150
151
    #[Groups(['blog:read'])]
152
    public function getCreatedAt(): string
153
    {
154
        return $this->getDateCreation()->format('Y-m-d');
155
    }
156
157
    #[Groups(['blog:read'])]
158
    public function getOwner(): array
159
    {
160
        if (method_exists($this, 'getCreator') && null !== $this->getCreator()) {
161
            $u = $this->getCreator();
162
            $name = method_exists($u, 'getCompleteName') ? $u->getCompleteName()
163
                : (method_exists($u, 'getUsername') ? $u->getUsername() : 'Owner');
164
165
            return [
166
                'id'   => method_exists($u, 'getId') ? $u->getId() : null,
167
                'name' => $name,
168
            ];
169
        }
170
171
        return ['id' => null, 'name' => 'Owner'];
172
    }
173
174
    #[Groups(['blog:read', 'blog:write'])]
175
    public function getVisible(): bool
176
    {
177
        $link = $this->getFirstResourceLink();
178
        if (!$link instanceof ResourceLink) {
179
            return true;
180
        }
181
182
        return ResourceLink::VISIBILITY_PUBLISHED === $link->getVisibility();
183
    }
184
185
    public function setVisible(bool $visible): self
186
    {
187
        $link = $this->getFirstResourceLink();
188
        if ($link instanceof ResourceLink) {
189
            $link->setVisibility(
190
                $visible ? ResourceLink::VISIBILITY_PUBLISHED : ResourceLink::VISIBILITY_DRAFT
191
            );
192
        }
193
        return $this;
194
    }
195
196
    #[Groups(['blog:read'])]
197
    public function getVisibilityName(): string
198
    {
199
        $link = $this->getFirstResourceLink();
200
        if ($link instanceof ResourceLink) {
201
            return (string) $link->getVisibilityName();
202
        }
203
204
        return 'published';
205
    }
206
207
    // === ResourceInterface ===
208
    public function getResourceIdentifier(): int { return (int) ($this->getIid() ?? 0); }
209
    public function getResourceName(): string { return $this->getTitle(); }
210
    public function setResourceName(string $name): self { return $this->setTitle($name); }
211
212
    public function __toString(): string { return $this->getTitle(); }
213
}
214