Completed
Push — master ( 57b021...d38ccd )
by Nicolas
29:22 queued 27:52
created

ImageTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 0
dl 0
loc 76
ccs 0
cts 14
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getImage() 0 4 1
A setImage() 0 6 1
A hasImage() 0 4 1
A getImageFile() 0 4 1
A setImageFile() 0 12 2
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