Completed
Pull Request — master (#8)
by Nicolas
28:01
created

ImageTrait::setImageFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 5
cp 0
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Smart\ContentBundle\Entity\Traits;
4
5
use Doctrine\ORM\Mapping as ORM;
6
use Symfony\Component\HttpFoundation\File\File;
7
use Symfony\Component\HttpFoundation\File\UploadedFile;
8
use Symfony\Component\Validator\Constraints as Assert;
9
use Vich\UploaderBundle\Mapping\Annotation as Vich;
10
11
/**
12
 * https://symfony.com/doc/master/bundles/EasyAdminBundle/integration/vichuploaderbundle.html
13
 */
14
trait ImageTrait
15
{
16
    /**
17
     * @var string
18
     *
19
     * @ORM\Column(type="text", nullable=true)
20
     */
21
    protected $image;
22
23
    /**
24
     * @Vich\UploadableField(mapping="smart_content_image", fileNameProperty="image")
25
     *
26
     * @Assert\Image
27
     * @Assert\File(maxSize="500k")
28
     *
29
     * @var File
30
     */
31
    protected $imageFile;
32
33
    /**
34
     * @return string
35
     */
36
    public function getImage()
37
    {
38
        return $this->image;
39
    }
40
41
    /**
42
     * @param string $image
43
     *
44
     * @return $this
45
     */
46
    public function setImage($image)
47
    {
48
        $this->image = $image;
49
50
        return $this;
51
    }
52
53
    /**
54
     * @return bool
55
     */
56
    public function hasImage()
57
    {
58
        return ($this->image !== null);
59
    }
60
61
    /**
62
     * @return File
63
     */
64
    public function getImageFile()
65
    {
66
        return $this->imageFile;
67
    }
68
69
    /**
70
     * It is required that at least one field changes if you are using Doctrine,
71
     * otherwise the event listeners won't be called and the file is lost
72
     *
73
     * @param File|UploadedFile $file
74
     *
75
     * @return $this
76
     */
77
    public function setImageFile(File $file = null)
78
    {
79
        $this->imageFile = $file;
80
81
        if ($file) {
82
            // It is required that at least one field changes if you are using doctrine
83
            // otherwise the event listeners won't be called and the file is lost
84
            $this->updatedAt = new \DateTime();
0 ignored issues
show
Bug introduced by
The property updatedAt does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
85
        }
86
87
        return $this;
88
    }
89
}
90