Passed
Push — develop ( 1acb2f...6f9b00 )
by BENARD
02:32
created

Article::getContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ProjetNormandie\ArticleBundle\Entity;
6
7
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
8
use ApiPlatform\Metadata\ApiFilter;
9
use ApiPlatform\Metadata\ApiResource;
10
use ApiPlatform\Metadata\GetCollection;
11
use ApiPlatform\Metadata\Get;
12
use Doctrine\Common\Collections\ArrayCollection;
13
use Doctrine\Common\Collections\Collection;
14
use Doctrine\ORM\Mapping as ORM;
15
use Gedmo\Timestampable\Traits\TimestampableEntity;
16
use ProjetNormandie\ArticleBundle\Repository\ArticleRepository;
17
use ProjetNormandie\ArticleBundle\ValueObject\ArticleStatus;
0 ignored issues
show
Bug introduced by
The type ProjetNormandie\ArticleB...lueObject\ArticleStatus was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Symfony\Component\Serializer\Attribute\Groups;
19
use Symfony\Component\Validator\Constraints as Assert;
20
use DateTime;
21
22
#[ORM\Table(name:'pna_article')]
23
#[ORM\Entity(repositoryClass: ArticleRepository::class)]
24
#[ORM\EntityListeners(["ProjetNormandie\ArticleBundle\EventListener\Entity\ArticleListener"])]
25
#[ApiResource(
26
    operations: [
27
        new GetCollection(),
28
        new Get(),
29
    ],
30
    normalizationContext: ['groups' => ['article:read', 'article:author', 'user:read']],
31
    order: ['publishedAt' => 'DESC']
32
)]
33
#[ApiFilter(
34
    SearchFilter::class,
35
    properties: [
36
        'status' => 'exact',
37
    ]
38
)]
39
class Article
40
{
41
    use TimestampableEntity;
42
43
    #[Groups(['article:read'])]
44
    #[ORM\Id, ORM\Column, ORM\GeneratedValue]
45
    private ?int $id = null;
46
47
    #[ORM\Column(length: 30, nullable: false)]
48
    private string $status = ArticleStatus::UNDER_CONSTRUCTION;
49
50
    #[Groups(['article:read'])]
51
    #[ORM\Column(nullable: false, options: ['default' => 0])]
52
    private int $nbComment = 0;
53
54
    #[Groups(['article:author'])]
55
    #[ORM\ManyToOne(targetEntity: UserInterface::class, fetch: 'EAGER')]
56
    #[ORM\JoinColumn(name:'author_id', referencedColumnName:'id', nullable:false)]
57
    private $author;
58
59
    #[Groups(['article:read'])]
60
    #[ORM\Column(nullable: true)]
61
    private ?DateTime $publishedAt = null;
62
63
    #[ORM\OneToMany(mappedBy: 'article', targetEntity: Comment::class)]
64
    private Collection $comments;
65
66
    #[ORM\OneToMany(
67
        mappedBy: 'translatable',
68
        targetEntity: ArticleTranslation::class,
69
        cascade: ['persist', 'remove'],
70
        fetch: 'EAGER',
71
        orphanRemoval: true,
72
        indexBy: 'locale'
73
    )]
74
    private Collection $translations;
75
76
    #[Groups(['article:read'])]
77
    #[ORM\Column(length: 255, unique: false)]
78
    private ?string $slug = null;
79
80
    private ?string $currentLocale = null;
81
82
    public function __construct()
83
    {
84
        $this->comments = new ArrayCollection();
85
        $this->translations = new ArrayCollection();
86
    }
87
88
    public function __toString()
89
    {
90
        return sprintf('%s [%s]', $this->getDefaultTitle(), $this->id);
91
    }
92
93
    public function getDefaultTitle(): string
94
    {
95
        return $this->getTitle('en') ?: 'Untitled';
96
    }
97
98
    public function getDefaultContent(): string
99
    {
100
        return $this->getContent('en') ?: '';
101
    }
102
103
    public function setId(int $id): void
104
    {
105
        $this->id = $id;
106
    }
107
108
    public function getId(): ?int
109
    {
110
        return $this->id;
111
    }
112
113
    public function setStatus(string $status): void
114
    {
115
        $this->status = $status;
116
    }
117
118
    public function getStatus(): string
119
    {
120
        return $this->status;
121
    }
122
123
    public function getArticleStatus(): ArticleStatus
124
    {
125
        return new ArticleStatus($this->status);
126
    }
127
128
    public function setNbComment(int $nbComment): void
129
    {
130
        $this->nbComment = $nbComment;
131
    }
132
133
    public function getNbComment(): int
134
    {
135
        return $this->nbComment;
136
    }
137
138
    public function getAuthor()
139
    {
140
        return $this->author;
141
    }
142
143
    public function setAuthor($author): void
144
    {
145
        $this->author = $author;
146
    }
147
148
    public function getPublishedAt(): ?DateTime
149
    {
150
        return $this->publishedAt;
151
    }
152
153
    public function setPublishedAt(?DateTime $publishedAt = null): void
154
    {
155
        $this->publishedAt = $publishedAt;
156
    }
157
158
    public function setComments(Collection $comments): void
159
    {
160
        $this->comments = $comments;
161
    }
162
163
    public function getComments(): Collection
164
    {
165
        return $this->comments;
166
    }
167
168
    public function getSlug(): ?string
169
    {
170
        return $this->slug;
171
    }
172
173
    public function setSlug(string $slug): void
174
    {
175
        $this->slug = $slug;
176
    }
177
178
    // Translation methods for A2lix compatibility
179
    public function getTranslations(): Collection
180
    {
181
        return $this->translations;
182
    }
183
184
    public function setTranslations(Collection $translations): self
185
    {
186
        $this->translations = $translations;
187
        return $this;
188
    }
189
190
    public function addTranslation(ArticleTranslation $translation): self
191
    {
192
        if (!$this->translations->contains($translation)) {
193
            $translation->setTranslatable($this);
194
            $this->translations->set($translation->getLocale(), $translation);
195
        }
196
197
        return $this;
198
    }
199
200
    public function removeTranslation(ArticleTranslation $translation): self
201
    {
202
        if ($this->translations->removeElement($translation)) {
203
            $translation->setTranslatable(null);
204
        }
205
206
        return $this;
207
    }
208
209
    public function translate(?string $locale = null, bool $fallbackToDefault = true): ArticleTranslation
0 ignored issues
show
Unused Code introduced by
The parameter $fallbackToDefault is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

209
    public function translate(?string $locale = null, /** @scrutinizer ignore-unused */ bool $fallbackToDefault = true): ArticleTranslation

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
210
    {
211
        $locale = $locale ?: $this->currentLocale ?: 'en';
212
213
        if (!$this->translations->containsKey($locale)) {
214
            $translation = new ArticleTranslation();
215
            $translation->setTranslatable($this);
216
            $translation->setLocale($locale);
217
            $this->translations->set($locale, $translation);
218
        }
219
220
        return $this->translations->get($locale);
221
    }
222
223
    public function setCurrentLocale(string $locale): void
224
    {
225
        $this->currentLocale = $locale;
226
    }
227
228
    public function getCurrentLocale(): ?string
229
    {
230
        return $this->currentLocale;
231
    }
232
233
    public function setTitle(string $title, ?string $locale = null): void
234
    {
235
        $this->translate($locale)->setTitle($title);
236
    }
237
238
    #[Groups(['article:read'])]
239
    public function getTitle(?string $locale = null): ?string
240
    {
241
        return $this->translate($locale)->getTitle();
242
    }
243
244
    public function setContent(string $content, ?string $locale = null): void
245
    {
246
        $this->translate($locale)->setContent($content);
247
    }
248
249
    #[Groups(['article:read'])]
250
    public function getContent(?string $locale = null): ?string
251
    {
252
        return $this->translate($locale)->getContent();
253
    }
254
255
256
    // Old methods for backward compatibility
257
    public function mergeNewTranslations(): void
258
    {
259
        // Not needed anymore
260
    }
261
}
262