Test Failed
Pull Request — master (#7)
by
unknown
02:32
created

Local   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 12
eloc 20
c 3
b 0
f 0
dl 0
loc 60
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A delete() 0 14 3
A getFilePath() 0 3 1
A ensureDirectory() 0 3 2
A __construct() 0 3 1
A upload() 0 16 5
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