Picture::removeUpload()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Entity;
4
5
use Doctrine\ORM\Mapping as ORM;
6
7
use App\Model\Picture as PictureModel;
8
9
/**
10
 * @ORM\Entity()
11
 * @ORM\Table(name="pictures")
12
 * @ORM\HasLifecycleCallbacks
13
 */
14
class Picture extends PictureModel
15
{
16
    const UPLOAD_DIR = 'images/uploads';
17
    /**
18
     * @ORM\Id
19
     * @ORM\Column(type="integer")
20
     * @ORM\GeneratedValue(strategy="AUTO")
21
     */
22
    protected $id;
23
    /**
24
     * @ORM\Column(type="string", length=255)
25
     */
26
    protected $name;
27
    /**
28
     * @ORM\Column(type="string", length=255)
29
     */
30
    protected $path;
31
32
    public function setId(int $id): Picture
33
    {
34
        $this->id = $id;
35
        
36
        return $this;
37
    }
38
    
39
    public function getId(): int
40
    {
41
        return $this->id;
42
    }
43
    
44
    /**
45
     * @ORM\PrePersist()
46
     * @ORM\PreUpdate()
47
     */
48 1
    public function preUpload()
49
    {
50 1
        if (null !== $this->file) {
51 1
            $this->path = "{$this->name}.{$this->file->guessExtension()}";
52
        }
53 1
    }
54
55
    /**
56
     * @ORM\PostPersist()
57
     * @ORM\PostUpdate()
58
     */
59 1
    public function upload()
60
    {
61 1
        if (null === $this->file) {
62
            return;
63
        }
64
65
        // check if we have an old image
66 1
        if (isset($this->temp)) {
67
            // delete the old image
68
            unlink($this->temp);
0 ignored issues
show
Bug introduced by
$this->temp of type resource is incompatible with the type string expected by parameter $filename of unlink(). ( Ignorable by Annotation )

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

68
            unlink(/** @scrutinizer ignore-type */ $this->temp);
Loading history...
69
            // clear the temp image path
70
            $this->temp = null;
71
        }
72
        // you must throw an exception here if the file cannot be moved
73
        // so that the entity is not persisted to the database
74
        // which the UploadedFile move() method does
75 1
        $this->file->move(
76 1
            $this->getUploadRootDir(),
77 1
            $this->path
78
        );
79 1
        $this->setFile(null);
80 1
    }
81
82
    /**
83
     * @ORM\PreRemove()
84
     */
85
    public function storeFilenameForRemove()
86
    {
87
        $this->temp = $this->getAbsolutePath();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getAbsolutePath() of type string is incompatible with the declared type resource of property $temp.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
88
    }
89
90
    /**
91
     * @ORM\PostRemove()
92
     */
93
    public function removeUpload()
94
    {
95
        if (isset($this->temp)) {
96
            unlink($this->temp);
0 ignored issues
show
Bug introduced by
$this->temp of type resource is incompatible with the type string expected by parameter $filename of unlink(). ( Ignorable by Annotation )

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

96
            unlink(/** @scrutinizer ignore-type */ $this->temp);
Loading history...
97
        }
98
    }
99
}