Completed
Push — develop ( bb7f99...d04bd8 )
by Victor
03:30
created

Article::setPictureBig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
namespace AppBundle\Entity;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\ORM\Mapping as ORM;
7
use Gedmo\Mapping\Annotation as Gedmo;
8
use Symfony\Component\HttpFoundation\File\UploadedFile;
9
use Symfony\Component\Validator\Constraints as Assert;
10
11
/**
12
 * Article
13
 *
14
 * @ORM\Table(name="article")
15
 * @ORM\Entity(repositoryClass="AppBundle\Repository\ArticleRepository")
16
 * @ORM\HasLifecycleCallbacks
17
 */
18
class Article
19
{
20
    /**
21
     * @var int
22
     *
23
     * @ORM\Column(name="id", type="integer")
24
     * @ORM\Id
25
     * @ORM\GeneratedValue(strategy="AUTO")
26
     */
27
    private $id;
28
29
    /**
30
     * @var string
31
     *
32
     * @ORM\Column(name="title", type="string", length=100)
33
     *
34
     * @Assert\NotBlank()
35
     * @Assert\Length(max = 100)
36
     */
37
    private $title;
38
39
    /**
40
     * @var string
41
     *
42
     * @Gedmo\Slug(fields={"title"}, updatable=true, separator="_")
43
     * @ORM\Column(name="slug", type="string", length=100)
44
     *
45
     * @Assert\Length(max = 100)
46
     */
47
    private $slug;
48
49
    /**
50
     * @ORM\ManyToOne(targetEntity="User", inversedBy="articles")
51
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
52
     *
53
     * @Assert\NotBlank()
54
     */
55
    private $user;
56
57
    /**
58
     * @var string
59
     *
60
     * @ORM\Column(name="picture", type="string", length=150, nullable=true)
61
     *
62
     * @Assert\Length(max = 150)
63
     */
64
    private $picture;
65
66
    /**
67
     * @Assert\File(maxSize="6000000",
68
     *     mimeTypes={
69
     *     "image/jpeg",
70
     *     "image/jpg",
71
     *     "image/png"})
72
     */
73
    private $file;
74
75
    private $temp;
76
77
    /**
78
     * @var string
79
     *
80
     * @ORM\Column(name="text", type="text")
81
     *
82
     * @Assert\Length(max = 10000)
83
     */
84
    private $text;
85
86
    /**
87
     * @ORM\ManyToMany(targetEntity="Tag", inversedBy="articles")
88
     * @ORM\JoinTable(name="articles_tags")
89
     */
90
    private $tags;
91
92
    /**
93
     * @ORM\ManyToMany(targetEntity="Category", inversedBy="articles")
94
     * @ORM\JoinTable(name="articles_categories")
95
     */
96
    private $categories;
97
98
    /**
99
     * @ORM\OneToMany(targetEntity="Comment", mappedBy="article", cascade={"remove"})
100
     */
101
    private $comments;
102
103
    /**
104
     * @var \DateTime
105
     *
106
     * @Gedmo\Timestampable(on="create")
107
     * @ORM\Column(name="created_at", type="datetime")
108
     *
109
     * @Assert\DateTime()
110
     */
111
    private $createdAt;
112
113
    /**
114
     * @var \DateTime
115
     *
116
     * @Gedmo\Timestampable(on="change", field={"title", "text"})
117
     * @ORM\Column(name="updated_at", type="datetime", nullable=true)
118
     *
119
     * @Assert\DateTime()
120
     */
121
    private $updatedAt;
122
123
    /**
124
     * @var \DateTime
125
     *
126
     * @ORM\Column(name="deleted_at", type="datetime", nullable=true)
127
     *
128
     * @Assert\DateTime()
129
     */
130
    private $deletedAt;
131
132
133
    /**
134
     * Constructor
135
     */
136
    public function __construct()
137
    {
138
        $this->tags = new ArrayCollection();
139
        $this->categories = new ArrayCollection();
140
        $this->comments = new ArrayCollection();
141
    }
142
143
    /**
144
     * Get id
145
     *
146
     * @return int
147
     */
148
    public function getId()
149
    {
150
        return $this->id;
151
    }
152
153
    /**
154
     * Set title
155
     *
156
     * @param string $title
157
     *
158
     * @return Article
159
     */
160
    public function setTitle($title)
161
    {
162
        $this->title = $title;
163
164
        return $this;
165
    }
166
167
    /**
168
     * Get title
169
     *
170
     * @return string
171
     */
172 5
    public function getTitle()
173
    {
174 5
        return $this->title;
175
    }
176
177
    /**
178
     * Set slug
179
     *
180
     * @param string $slug
181
     *
182
     * @return Article
183
     */
184
    public function setSlug($slug)
185
    {
186
        $this->slug = $slug;
187
188
        return $this;
189
    }
190
191
    /**
192
     * Get slug
193
     *
194
     * @return string
195
     */
196 5
    public function getSlug()
197
    {
198 5
        return $this->slug;
199
    }
200
201
    /**
202
     * Set user
203
     *
204
     * @param string $user
205
     *
206
     * @return Article
207
     */
208
    public function setUser($user)
209
    {
210
        $this->user = $user;
211
212
        return $this;
213
    }
214
215
    /**
216
     * Get user
217
     *
218
     * @return string
219
     */
220 6
    public function getUser()
221
    {
222 6
        return $this->user;
223
    }
224
225
    /**
226
     * Set text
227
     *
228
     * @param string $text
229
     *
230
     * @return Article
231
     */
232
    public function setText($text)
233
    {
234
        $this->text = $text;
235
236
        return $this;
237
    }
238
239
    /**
240
     * Get text
241
     *
242
     * @return string
243
     */
244 5
    public function getText()
245
    {
246 5
        return $this->text;
247
    }
248
249
    /**
250
     * Set createdAt
251
     *
252
     * @param \DateTime $createdAt
253
     *
254
     * @return Article
255
     */
256
    public function setCreatedAt($createdAt)
257
    {
258
        $this->createdAt = $createdAt;
259
260
        return $this;
261
    }
262
263
    /**
264
     * Get createdAt
265
     *
266
     * @return \DateTime
267
     */
268 5
    public function getCreatedAt()
269
    {
270 5
        return $this->createdAt;
271
    }
272
273
    /**
274
     * Set updatedAt
275
     *
276
     * @param \DateTime $updatedAt
277
     *
278
     * @return Article
279
     */
280
    public function setUpdatedAt($updatedAt)
281
    {
282
        $this->updatedAt = $updatedAt;
283
284
        return $this;
285
    }
286
287
    /**
288
     * Get updatedAt
289
     *
290
     * @return \DateTime
291
     */
292
    public function getUpdatedAt()
293
    {
294
        return $this->updatedAt;
295
    }
296
297
    /**
298
     * Set deletedAt
299
     *
300
     * @param \DateTime $deletedAt
301
     *
302
     * @return Article
303
     */
304
    public function setDeletedAt($deletedAt)
305
    {
306
        $this->deletedAt = $deletedAt;
307
308
        return $this;
309
    }
310
311
    /**
312
     * Get deletedAt
313
     *
314
     * @return \DateTime
315
     */
316
    public function getDeletedAt()
317
    {
318
        return $this->deletedAt;
319
    }
320
321
    /**
322
     * Add tag
323
     *
324
     * @param \AppBundle\Entity\Tag $tag
325
     *
326
     * @return Article
327
     */
328
    public function addTag(Tag $tag)
329
    {
330
        $this->tags[] = $tag;
331
332
        return $this;
333
    }
334
335
    /**
336
     * Remove tag
337
     *
338
     * @param \AppBundle\Entity\Tag $tag
339
     */
340
    public function removeTag(Tag $tag)
341
    {
342
        $this->tags->removeElement($tag);
343
    }
344
345
    /**
346
     * Get tags
347
     *
348
     * @return \Doctrine\Common\Collections\Collection
349
     */
350 5
    public function getTags()
351
    {
352 5
        return $this->tags;
353
    }
354
355
    /**
356
     * Add category
357
     *
358
     * @param \AppBundle\Entity\Category $category
359
     *
360
     * @return Article
361
     */
362
    public function addCategory(Category $category)
363
    {
364
        $this->categories[] = $category;
365
366
        return $this;
367
    }
368
369
    /**
370
     * Remove category
371
     *
372
     * @param \AppBundle\Entity\Category $category
373
     */
374
    public function removeCategory(Category $category)
375
    {
376
        $this->categories->removeElement($category);
377
    }
378
379
    /**
380
     * Get categories
381
     *
382
     * @return \Doctrine\Common\Collections\Collection
383
     */
384 5
    public function getCategories()
385
    {
386 5
        return $this->categories;
387
    }
388
389
    /**
390
     * Add comment
391
     *
392
     * @param \AppBundle\Entity\Comment $comment
393
     *
394
     * @return Article
395
     */
396
    public function addComment(Comment $comment)
397
    {
398
        $this->comments[] = $comment;
399
400
        return $this;
401
    }
402
403
    /**
404
     * Remove comment
405
     *
406
     * @param \AppBundle\Entity\Comment $comment
407
     */
408
    public function removeComment(Comment $comment)
409
    {
410
        $this->comments->removeElement($comment);
411
    }
412
413
    /**
414
     * Get comments
415
     *
416
     * @return \Doctrine\Common\Collections\Collection
417
     */
418
    public function getComments()
419
    {
420
        return $this->comments;
421
    }
422
423
    /**
424
     * Set picture
425
     *
426
     * @param string $picture
427
     *
428
     * @return Article
429
     */
430
    public function setPictureSmall($picture)
431
    {
432
        $this->picture = $picture;
433
434
        return $this;
435
    }
436
437
    /**
438
     * Get pictureSmall
439
     *
440
     * @return string
441
     */
442
    public function getPicture()
443
    {
444
        return $this->picture;
445
    }
446
    
447
    
448
    /**
449
     * ***********************
450
     * Upload images start
451
     * ***********************
452
     */
453
454
    /**
455
     * Sets file.
456
     *
457
     * @param UploadedFile $file
458
     */
459 View Code Duplication
    public function setFile(UploadedFile $file = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
460
    {
461
        $this->file = $file;
462
        if (isset($this->picture)) {
463
            $this->temp = $this->picture;
464
            $this->picture = null;
465
        } else {
466
            $this->picture = 'initial';
467
        }
468
    }
469
470
    /**
471
     * Get file.
472
     *
473
     * @return UploadedFile
474
     */
475
    public function getFile()
476
    {
477
        return $this->file;
478
    }
479
480
    public function getAbsolutePath()
481
    {
482
        return null === $this->picture
483
            ? null
484
            : $this->getUploadRootDir().'/'.$this->picture;
485
    }
486
487 5
    public function getWebPath()
488
    {
489 5
        return null === $this->picture
490 5
            ? null
491 5
            : $this->getUploadDir().'/'.$this->picture;
492
    }
493
494
    protected function getUploadRootDir()
495
    {
496
        return __DIR__.'/../../../web/'.$this->getUploadDir();
497
    }
498
499 5
    protected function getUploadDir()
500
    {
501 5
        return 'media/posts';
502
    }
503
504
    /**
505
     * @ORM\PrePersist()
506
     * @ORM\PreUpdate()
507
     */
508 View Code Duplication
    public function preUpload()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
509
    {
510
        if (null !== $this->getFile()) {
511
            $filename = sha1(uniqid(mt_rand(), true));
512
            $this->picture = $filename.'.'.$this->getFile()->guessExtension();
513
        }
514
    }
515
516
    /**
517
     * @ORM\PostPersist()
518
     * @ORM\PostUpdate()
519
     */
520 View Code Duplication
    public function upload()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
521
    {
522
        if (null === $this->getFile()) {
523
            return;
524
        }
525
526
        $this->getFile()->move($this->getUploadRootDir(), $this->picture);
527
528
        if (isset($this->temp)) {
529
            unlink($this->getUploadRootDir().'/'.$this->temp);
530
531
            $this->clearCache($this->getUploadDir().'/'.$this->temp);
532
533
            $this->temp = null;
534
        }
535
        $this->file = null;
536
    }
537
538
    /**
539
     * @ORM\PostRemove()
540
     */
541
    public function removeUpload()
542
    {
543
        $file = $this->getAbsolutePath();
544
        if ($file) {
545
            unlink($file);
546
547
            $this->clearCache($this->getWebPath());
548
        }
549
    }
550
551
    public function clearCache($path)
0 ignored issues
show
Coding Style introduced by
clearCache uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
552
    {
553
        $kernel = $GLOBALS['kernel'];
554
        $cacheManager = $kernel->getContainer()->get('liip_imagine.cache.manager');
555
        $cacheManager->remove($path);
556
    }
557
558
    /**
559
     * ***********************
560
     * Upload images end
561
     * ***********************
562
     */
563
}
564