Advert::getAuthor()   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
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace App\Entity;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\Collections\Collection;
7
use Doctrine\ORM\Mapping as ORM;
8
use Symfony\Component\Validator\Constraints as Assert;
9
use ApiPlatform\Core\Annotation\ApiResource;
10
11
/**
12
 * @ORM\Entity(repositoryClass="App\Repository\AdvertRepository")
13
 * @ORM\HasLifecycleCallbacks()
14
 * @ApiResource
15
 */
16
class Advert
17
{
18
    /**
19
     * @ORM\Id()
20
     * @ORM\GeneratedValue()
21
     * @ORM\Column(type="integer")
22
     */
23
    private $id;
24
25
    /**
26
     * @ORM\Column(type="date")
27
     */
28
    private $date;
29
30
    /**
31
     * @ORM\Column(name="updated_at", type="datetime", nullable=true)
32
     */
33
    private $updatedAt;
34
35
    /**
36
     * @ORM\Column(type="string", length=255)
37
     * @Assert\NotBlank
38
     */
39
    private $title;
40
41
    /**
42
     * @ORM\Column(type="text")
43
     * @Assert\NotBlank
44
     */
45
    private $content;
46
47
    /**
48
     * @ORM\Column(type="boolean")
49
     */
50
    private $published = true;
51
52
    /**
53
     * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="adverts", cascade={"persist"})
54
     * @ORM\JoinColumn(nullable=false)
55
     */
56
    private $author;
57
58
    /**
59
     * @ORM\OneToMany(targetEntity="App\Entity\Application", mappedBy="advert")
60
     */
61
    private $applications;
62
63
    /**
64
     * @ORM\ManyToMany(targetEntity="App\Entity\Category", inversedBy="adverts", cascade={"persist"})
65
     * @ORM\JoinTable(name="advert_category")
66
     */
67
    private $categories;
68
69
    /**
70
     * @ORM\ManyToOne(targetEntity="App\Entity\Department", inversedBy="adverts")
71
     */
72
    private $department;
73
74
    /**
75
     * @ORM\Column(name="nb_applications", type="integer")
76
     */
77
    private $nbApplications = 0;
78
79
    public function __construct()
80
    {
81
        $this->applications = new ArrayCollection();
82
        $this->categories = new ArrayCollection();
83
    }
84
85
    public function getId(): ?int
86
    {
87
        return $this->id;
88
    }
89
90
    public function getDate(): ?\DateTimeInterface
91
    {
92
        return $this->date;
93
    }
94
95
    public function setDate(\DateTimeInterface $date): self
96
    {
97
        $this->date = $date;
98
99
        return $this;
100
    }
101
102
    public function getTitle(): ?string
103
    {
104
        return $this->title;
105
    }
106
107
    public function setTitle(string $title): self
108
    {
109
        $this->title = $title;
110
111
        return $this;
112
    }
113
114
    public function getContent(): ?string
115
    {
116
        return $this->content;
117
    }
118
119
    public function setContent(string $content): self
120
    {
121
        $this->content = $content;
122
123
        return $this;
124
    }
125
126
    public function getPublished(): ?bool
127
    {
128
        return $this->published;
129
    }
130
131
    public function setPublished(bool $published): self
132
    {
133
        $this->published = $published;
134
135
        return $this;
136
    }
137
138
    public function getAuthor(): ?User
139
    {
140
        return $this->author;
141
    }
142
143
    public function setAuthor(?User $author): self
144
    {
145
        $this->author = $author;
146
147
        return $this;
148
    }
149
150
    /**
151
     * @return Collection|Application[]
152
     */
153
    public function getApplications(): Collection
154
    {
155
        return $this->applications;
156
    }
157
158
    public function addApplication(Application $application): self
159
    {
160
        if (!$this->applications->contains($application)) {
161
            $this->applications[] = $application;
162
            $application->setAdvert($this);
163
        }
164
165
        return $this;
166
    }
167
168
    public function removeApplication(Application $application): self
169
    {
170
        if ($this->applications->contains($application)) {
171
            $this->applications->removeElement($application);
172
            // set the owning side to null (unless already changed)
173
            if ($application->getAdvert() === $this) {
174
                $application->setAdvert(null);
175
            }
176
        }
177
178
        return $this;
179
    }
180
181
    /**
182
     * @return Collection|Category[]
183
     */
184
    public function getCategories(): Collection
185
    {
186
        return $this->categories;
187
    }
188
189
    public function addCategory(Category $category): self
190
    {
191
        if (!$this->categories->contains($category)) {
192
            $this->categories[] = $category;
193
        }
194
195
        return $this;
196
    }
197
198
    public function removeCategory(Category $category): self
199
    {
200
        if ($this->categories->contains($category)) {
201
            $this->categories->removeElement($category);
202
        }
203
204
        return $this;
205
    }
206
207
    public function getDepartment(): ?Department
208
    {
209
        return $this->department;
210
    }
211
212
    public function setDepartment(?Department $department): self
213
    {
214
        $this->department = $department;
215
216
        return $this;
217
    }
218
219
    /**
220
     * @ORM\PreUpdate
221
     */
222
    public function updateDate()
223
    {
224
        $this->setUpdatedAt(new \Datetime());
225
    }
226
227
    public function increaseApplication()
228
    {
229
        $this->nbApplications++;
230
    }
231
232
    public function decreaseApplication()
233
    {
234
        $this->nbApplications--;
235
    }
236
237
    /**
238
     * @Assert\Callback
239
     */
240
    /*public function isContentValid(ExecutionContextInterface $context)
241
    {
242
        $forbiddenWords = array('démotivation', 'abandon');
243
244
        // On vérifie que le contenu ne contient pas l'un des mots
245
        if (preg_match('#'.implode('|', $forbiddenWords).'#', $this->getContent())) {
246
            // La règle est violée, on définit l'erreur
247
            $context
248
                ->buildViolation('Contenu invalide car il contient un mot interdit.') // message
249
                ->atPath('content')                                                   // attribut de l'objet qui est violé
250
                ->addViolation() // ceci déclenche l'erreur, ne l'oubliez pas
251
            ;
252
        }
253
    }*/
254
255
    /**
256
     * @param \DateTime $updatedAt
257
     */
258
    public function setUpdatedAt(\Datetime $updatedAt = null)
259
    {
260
        $this->updatedAt = $updatedAt;
261
    }
262
263
    /**
264
     * @return \DateTime
265
     */
266
    public function getUpdatedAt()
267
    {
268
        return $this->updatedAt;
269
    }
270
271
    /**
272
     * @param integer $nbApplications
273
     */
274
    public function setNbApplications($nbApplications)
275
    {
276
        $this->nbApplications = $nbApplications;
277
    }
278
279
    /**
280
     * @return integer
281
     */
282
    public function getNbApplications()
283
    {
284
        return $this->nbApplications;
285
    }
286
}
287