Completed
Push — capistrano-3 ( 15aaa8...b5fc70 )
by Tijs
09:29 queued 06:48
created

AbstractFile::getFileName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace SumoCoders\FrameworkCoreBundle\ValueObject;
4
5
use Doctrine\ORM\Mapping as ORM;
6
use Symfony\Component\HttpFoundation\File\UploadedFile;
7
8
/**
9
 * The following things are mandatory to use this class.
10
 *
11
 * You need to implement the method getUploadDir.
12
 * When using this class in an entity certain life cycle callbacks should be called
13
 * prepareToUpload for @ORM\PrePersist() and @ORM\PreUpdate()
14
 * upload for @ORM\PostPersist() and @ORM\PostUpdate()
15
 * remove for @ORM\PostRemove()
16
 */
17
abstract class AbstractFile
18
{
19
    /**
20
     * @var string
21
     * @ORM\Column(type="string", length=255, nullable=true)
22
     */
23
    protected $fileName = null;
24
25
    /**
26
     * @var UploadedFile
27
     */
28
    protected $file;
29
30
    /**
31
     * @var string
32
     */
33
    protected $oldFileName;
34
35
    /**
36
     * @param string $fileName
37
     */
38
    protected function __construct($fileName)
39
    {
40
        $this->fileName = $fileName;
41
    }
42
43
    /**
44
     * @return string
45
     */
46
    public function getFileName()
47
    {
48
        return $this->fileName;
49
    }
50
51
    /**
52
     * @return string|null
53
     */
54
    public function getAbsolutePath()
55
    {
56
        return $this->fileName === null ? null : $this->getUploadRootDir() . '/' . $this->fileName;
57
    }
58
59
    /**
60
     * @return string
61
     */
62
    public function getWebPath()
63
    {
64
        $file = $this->getAbsolutePath();
65
        if (is_file($file) && file_exists($file)) {
66
            return '/' . $this->getUploadDir() . '/' . $this->fileName;
67
        }
68
69
        return '';
70
    }
71
72
    /**
73
     * @return string
74
     */
75
    protected function getUploadRootDir()
76
    {
77
        // the absolute directory path where uploaded documents should be saved
78
        return __DIR__ . '/../../../../web/' . $this->getTrimmedUploadDir();
79
    }
80
81
    /**
82
     * @return string
83
     */
84
    protected function getTrimmedUploadDir()
85
    {
86
        return trim($this->getUploadDir(), '/\\');
87
    }
88
89
    /**
90
     * The dir in the web folder where the file needs to be uploaded.
91
     * The base directory is always the src/Frontend/Files/ directory
92
     *
93
     * @return string
94
     */
95
    abstract protected function getUploadDir();
96
97
    /**
98
     * Sets file.
99
     *
100
     * @param UploadedFile $file
101
     *
102
     * @return static
103
     */
104
    public function setFile(UploadedFile $file = null)
105
    {
106
        if ($file === null) {
107
            return $this;
108
        }
109
110
        $this->file = $file;
111
        // check if we have an old image path
112
        if ($this->fileName === null) {
113
            return $this;
114
        }
115
116
        // store the old name to delete after the update
117
        $this->oldFileName = $this->fileName;
118
        $this->fileName = null;
119
120
        return clone $this;
121
    }
122
123
    /**
124
     * @param UploadedFile|null $uploadedFile
125
     *
126
     * @return static
127
     */
128
    public static function fromUploadedFile(UploadedFile $uploadedFile = null)
129
    {
130
        $file = new static(null);
131
        $file->setFile($uploadedFile);
132
133
        return $file;
134
    }
135
136
    /**
137
     * Get file.
138
     *
139
     * @return UploadedFile
140
     */
141
    public function getFile()
142
    {
143
        return $this->file;
144
    }
145
146
    /**
147
     * This function should be called for the life cycle events @ORM\PrePersist() and @ORM\PreUpdate()
148
     */
149
    public function prepareToUpload()
150
    {
151
        if ($this->getFile() === null) {
152
            return;
153
        }
154
155
        // do whatever you want to generate a unique name
156
        $filename = sha1(uniqid(mt_rand(), true));
157
        $this->fileName = $filename . '.' . $this->getFile()->guessExtension();
158
    }
159
160
    /**
161
     * This function should be called for the life cycle events @ORM\PostPersist() and @ORM\PostUpdate()
162
     */
163
    public function upload()
164
    {
165
        // check if we have an old image
166
        if ($this->oldFileName !== null) {
167
            // delete the old image
168
            $oldFile = $this->getUploadRootDir() . '/' . $this->oldFileName;
169
            if (is_file($oldFile) && file_exists($oldFile)) {
170
                unlink($oldFile);
171
            }
172
            // clear the $this->oldFileName image path
173
            $this->oldFileName = null;
174
        }
175
176
        if ($this->getFile() === null) {
177
            return;
178
        }
179
180
        $this->writeFileToDisk();
181
182
        $this->file = null;
183
    }
184
185
    /**
186
     * if there is an error when moving the file, an exception will
187
     * be automatically thrown by move(). This will properly prevent
188
     * the entity from being persisted to the database on error
189
     */
190
    protected function writeFileToDisk()
191
    {
192
        $this->getFile()->move($this->getUploadRootDir(), $this->fileName);
193
    }
194
195
    /**
196
     * This function should be called for the life cycle event @ORM\PostRemove()
197
     */
198
    public function remove()
199
    {
200
        $file = $this->getAbsolutePath();
201
        if (!is_file($file) || !file_exists($file)) {
202
            return;
203
        }
204
205
        unlink($file);
206
    }
207
208
    /**
209
     * Returns a string representation of the image.
210
     *
211
     * @return string
212
     */
213
    public function __toString()
214
    {
215
        return (string) $this->fileName;
216
    }
217
218
    /**
219
     * @param string $fileName
220
     *
221
     * @return static
222
     */
223
    public static function fromString($fileName)
224
    {
225
        return new static($fileName);
226
    }
227
228
    /**
229
     * {@inheritdoc}
230
     */
231
    public function jsonSerialize()
232
    {
233
        return (string) $this->getWebPath();
234
    }
235
236
    public function markForDeletion()
237
    {
238
        $this->oldFileName = $this->fileName;
239
        $this->fileName = null;
240
    }
241
}
242