Passed
Push — master ( 9093f2...a966a4 )
by Taosikai
01:24 queued 17s
created

Local::delete()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 14
rs 10
cc 3
nc 4
nop 1
1
<?php
2
3
namespace Slince\Upload\Filesystem;
4
5
use \RuntimeException;
0 ignored issues
show
Bug introduced by
The type \RuntimeException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Slince\Upload\File;
7
use Symfony\Component\HttpFoundation\File\UploadedFile;
8
9
class Local implements FilesystemInterface
10
{
11
    /**
12
     * @var string
13
     */
14
    protected $savePath;
15
16
    public function __construct(string $savePath)
17
    {
18
        $this->savePath = rtrim($savePath, '\\/');
19
    }
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function upload(string $key, UploadedFile $file, bool $overwrite = false)
25
    {
26
        if (!$this->ensureDirectory($this->savePath)) {
27
            throw new \InvalidArgumentException(sprintf('Directory "%s" was not created', $this->savePath));
28
        }
29
30
        if (!is_writable($this->savePath)) {
31
            throw new \InvalidArgumentException(sprintf('The directory "%s" is invalid', $this->savePath));
32
        }
33
34
        $filePath = $this->getFilePath($key);
35
        if (file_exists($filePath) && !$overwrite) {
36
            throw new RuntimeException(sprintf('The file with key "%s" is exists.', $key));
37
        }
38
39
        return $file->move(dirname($filePath), basename($filePath));
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function delete(File $file): bool
46
    {
47
        $details = $file->getDetails();
48
49
        $filePath = '';
50
        if ($details) {
51
            $filePath = $details->getPathname();
52
        }
53
54
        if (file_exists($filePath)) {
55
            return unlink($filePath);
56
        }
57
58
        return true;
59
    }
60
61
    protected function getFilePath(string $key): string
62
    {
63
        return "{$this->savePath}/{$key}";
64
    }
65
66
    protected function ensureDirectory(string $directory): bool
67
    {
68
        return is_dir($directory) || mkdir($directory, 0755, true);
69
    }
70
}
71