Completed
Push — master ( a06a95...a1d542 )
by dan
15s
created

FileManager.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace IrishDan\ResponsiveImageBundle;
4
5
use Symfony\Component\Filesystem\Filesystem;
6
7
/**
8
 * Class FileSystem
9
 *
10
 * @package ResponsiveImageBundle
11
 */
12
class FileManager
13
{
14
    /**
15
     * @var $config
16
     */
17
    private $awsConfig;
18
    /**
19
     * @var string
20
     */
21
    private $rootDirectory;
22
    /**
23
     * @var string
24
     */
25
    private $stylesDirectory;
26
    /**
27
     * @var string
28
     */
29
    private $systemPath;
30
    /**
31
     * @var string
32
     */
33
    private $systemUploadPath;
34
    /**
35
     * @var string
36
     */
37
    private $systemStylesPath;
38
    /**
39
     * @var
40
     */
41
    private $tempDirectory = null;
42
    /**
43
     * @var
44
     */
45
    private $uploadsDirectory;
46
    /**
47
     * @var
48
     */
49
    private $webDirectory;
50
    /**
51
     * @var
52
     */
53
    private $webStylesDirectory;
54
    /**
55
     * @var Filesystem
56
     */
57
    private $fileSystem;
58
59
    /**
60
     * FileManager constructor.
61
     *
62
     * @param            $rootDirectory
63
     * @param array      $imageConfigs
64
     * @param Filesystem $fileSystem
65
     */
66
    public function __construct($rootDirectory, array $imageConfigs, FileSystem $fileSystem)
67
    {
68
        // @TODO: Replace with flysystem.
69
        $this->fileSystem = $fileSystem;
70
71
        $uploadsDirectory = $imageConfigs['image_directory'];
72
        $stylesDirectory = $imageConfigs['image_styles_directory'];
73
        $symfonyDirectory = substr($rootDirectory, 0, -4);
74
75
        $this->rootDirectory = $symfonyDirectory;
76
        $this->uploadsDirectory = $uploadsDirectory;
77
        $this->stylesDirectory = $uploadsDirectory . '/' . $stylesDirectory;
78
        $this->systemPath = $symfonyDirectory . '/web';
79
        $this->systemUploadPath = $this->systemPath . '/' . $this->uploadsDirectory;
80
        $this->systemStylesPath = $this->systemUploadPath . '/' . $stylesDirectory;
81
82
        // Set the temp directory if aws is enabled.
83
        if (!empty($imageConfigs['aws_s3'])) {
84
            if (!empty($imageConfigs['aws_s3']['enabled'])) {
85
                $this->awsConfig = $imageConfigs['aws_s3'];
86
                if (!empty($this->awsConfig['temp_directory'])) {
87
                    $this->tempDirectory = $symfonyDirectory . '/' . $this->awsConfig['temp_directory'];
88
                }
89
            }
90
        }
91
    }
92
93
    /**
94
     * @return string
95
     */
96
    public function getRootDirectory()
97
    {
98
        return $this->rootDirectory;
99
    }
100
101
    /**
102
     * @return mixed
103
     */
104
    public function getWebStylesDirectory()
105
    {
106
        return $this->webStylesDirectory;
107
    }
108
109
    /**
110
     * @return mixed
111
     */
112
    public function getWebDirectory()
113
    {
114
        return $this->webDirectory;
115
    }
116
117
    /**
118
     * @return string
119
     */
120
    public function getSystemStylesPath()
121
    {
122
        return $this->systemStylesPath;
123
    }
124
125
    /**
126
     * @return string
127
     */
128
    public function getSystemUploadPath()
129
    {
130
        return $this->systemUploadPath;
131
    }
132
133
    /**
134
     * @return string
135
     */
136
    public function getSystemPath()
137
    {
138
        return $this->systemPath;
139
    }
140
141
    /**
142
     * @return string
143
     */
144
    public function getStylesDirectory()
145
    {
146
        return $this->stylesDirectory;
147
    }
148
149
    /**
150
     * @return mixed
151
     */
152
    public function getUploadsDirectory()
153
    {
154
        return $this->uploadsDirectory;
155
    }
156
157
    /**
158
     * @param $stylename
159
     * @return string
160
     */
161
    public function getStyleTree($stylename)
162
    {
163
        return $this->stylesDirectory . '/' . $stylename;
164
    }
165
166
    /**
167
     * @return string
168
     */
169
    public function getSystemUploadDirectory()
170
    {
171
        return $this->systemUploadPath;
172
    }
173
174
    /**
175
     * Check if a directory exists in the system and optionally create it if it doesn't
176
     *
177
     * @param      $directory
178
     * @param bool $create
179
     * @return bool
180
     */
181
    public function directoryExists($directory, $create = false)
182
    {
183
        if (file_exists($directory)) {
184
            return true;
185
        } elseif (!file_exists($directory) && $create) {
186
            return mkdir($directory, 0775, true);
187
        } else {
188
            return false;
189
        }
190
    }
191
192
    /**
193
     * @param $fileName
194
     * @return mixed
195
     */
196
    public function fileExists($fileName)
197
    {
198
        $originalPath = $this->getStorageDirectory('original', $fileName);
199
200
        return file_exists($originalPath);
201
    }
202
203
    /**
204
     * Deletes a directory and its contents or a file.
205
     *
206
     * @param $target
207
     * @return bool
208
     */
209
    public function deleteDirectory($target)
210
    {
211
        if (is_dir($target)) {
212
            $files = glob($target . '/*');
213
            foreach ($files as $file) {
214
                $this->deleteFile($file);
215
            }
216
            rmdir($target);
217
        } elseif (is_file($target)) {
218
            $this->deleteFile($target);
219
        }
220
    }
221
222
    /**
223
     *
224
     */
225
    public function clearTemporaryFiles()
226
    {
227
        $temp = $this->getTempDirectory();
228
        $uploadsFolder = $this->getUploadsDirectory();
229
        $this->deleteDirectory($temp . $uploadsFolder);
230
    }
231
232
    /**
233
     * @param $path
234
     * @return bool
235
     */
236
    public function deleteFile($path)
237
    {
238
        // If path exists delete the file.
239
        if ($this->directoryExists($path)) {
240
            unlink($path);
241
        }
242
    }
243
244
    /**
245
     * @param $filename
246
     * @return string
247
     */
248
    public function uploadedFilePath($filename)
249
    {
250
        return $this->systemUploadPath . '/' . $filename;
251
    }
252
253
    /**
254
     * @param $stylename
255
     * @return string
256
     */
257
    public function styleDirectoryPath($stylename)
258
    {
259
        return $this->systemStylesPath . '/' . $stylename;
260
    }
261
262
    /**
263
     * @param $filename
264
     * @return string
265
     */
266
    public function uploadedFileWebPath($filename)
267
    {
268
        return $this->uploadsDir . '/' . $filename;
0 ignored issues
show
The property uploadsDir does not seem to exist. Did you mean uploadsDirectory?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
269
    }
270
271
    /**
272
     * @param $stylename
273
     * @param $filename
274
     * @return string
275
     */
276
    public function styleFilePath($stylename, $filename)
277
    {
278
        return $this->styleDirectoryPath($stylename) . '/' . $filename;
279
    }
280
281
    /**
282
     * @param $path
283
     * @return string
284
     */
285
    public function getFilenameFromPath($path)
286
    {
287
        return basename($path);
288
    }
289
290
    /**
291
     * @param $stylename
292
     * @return string
293
     */
294
    public function styleWebPath($stylename)
295
    {
296
        $stylesDirectory = $this->stylesDirectory;
297
        $path = $stylesDirectory . '/' . $stylename;
298
299
        return $path;
300
    }
301
302
    /**
303
     * Returns the web accessible styled file path.
304
     *
305
     * @param $stylename
306
     * @return string
307
     */
308
    public function styledFileWebPath($stylename, $filename)
309
    {
310
        $stylesDirectory = $this->stylesDirectory;
311
        $path = $stylesDirectory . '/' . $stylename . '/' . $filename;
312
313
        return $path;
314
    }
315
316
    /**
317
     * Return the temporary directory if it has been set.
318
     *
319
     * @return bool|string
320
     */
321
    public function getTempDirectory()
322
    {
323
        if ($this->tempDirectory != null) {
324
            $this->directoryExists($this->tempDirectory, true);
325
326
            return $this->tempDirectory;
327
        } else {
328
            return sys_get_temp_dir();
329
        }
330
    }
331
332
    /**
333
     * Returns the appropriate local storage directory based on the current operation and config parameters.
334
     *
335
     * @param $operation
336
     * @return string
337
     */
338
    public function getStorageDirectory($operation = 'original', $filename = null, $stylename = null)
339
    {
340
        // If AWS is not enabled the directory is the image_directory directory
341
        if (!empty($this->awsConfig) && !empty($this->awsConfig['enabled'])) {
342
            $remote_file_policy = $this->awsConfig['remote_file_policy'];
343
            switch ($operation) {
344
                case 'original':
345
                    if ($remote_file_policy == 'ALL') {
346
                        $directory = $this->getTempDirectory();
347
                    } else {
348
                        $directory = $this->getSystemUploadDirectory();
349
                    }
350
                    break;
351
                case 'styled':
352
                    $directory = $this->getTempDirectory();
353
                    break;
354
355
                case 'temporary':
356
                    // Use the temporary directory.
357
                    $directory = $this->getTempDirectory();
358
                    break;
359
360
                default:
361
                    // Use the web directory by default.
362
                    $directory = $this->getSystemUploadDirectory();
363
                    break;
364
            }
365
        } else {
366
            if (empty($stylename)) {
367
                $directory = $this->getSystemUploadDirectory();
368
            } else {
369
                $directory = $this->getSystemPath() . '/';
370
            }
371
        }
372
373
        if ($stylename !== null) {
374
            $styleTree = $this->getStyleTree($stylename);
375
            $directory .= $styleTree . '/';
376
        }
377
378
        // Check the trailing slash.
379
        $directory = $this->trailingSlash($directory, true);
380
381
        // Add the filename on the end.
382
        if (!empty($filename)) {
383
            $directory .= $filename;
384
        }
385
386
        return $directory;
387
    }
388
389
    /**
390
     * Ensures that a string has a trailing slash or not.
391
     *
392
     * @param      $path
393
     * @param bool $slash
394
     * @return string
395
     */
396
    protected function trailingSlash($path, $slash = true)
397
    {
398
        $path = rtrim($path, '/');
399
        if ($slash) {
400
            $path .= '/';
401
        }
402
403
        return $path;
404
    }
405
}