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

Flysystem::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Slince\Upload\Filesystem;
4
5
use League\Flysystem\Filesystem;
0 ignored issues
show
Bug introduced by
The type League\Flysystem\Filesystem 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 \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...
7
use Slince\Upload\File;
8
use Symfony\Component\HttpFoundation\File\UploadedFile;
9
10
class Flysystem implements FilesystemInterface
11
{
12
    /**
13
     * @var Filesystem
14
     */
15
    protected $filesystem;
16
17
    public function __construct(Filesystem $filesystem)
18
    {
19
        $this->filesystem = $filesystem;
20
    }
21
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function upload(string $key, UploadedFile $file, bool $overwrite = false)
26
    {
27
        try {
28
            $this->uploadToFlysystem($key, $file);
29
        } catch (RuntimeException $exception) {
30
            if (!$overwrite) {
31
                throw new \RuntimeException(sprintf('The file with key "%s" is exists.', $key));
32
            }
33
            $this->filesystem->delete($key);
34
            $this->uploadToFlysystem($key, $file);
35
        }
36
        @unlink($file->getPathname()); //remove old
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

36
        /** @scrutinizer ignore-unhandled */ @unlink($file->getPathname()); //remove old

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
37
        return true;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function delete(File $file): bool
44
    {
45
        $this->filesystem->delete($file->getName());
46
47
        return true;
48
    }
49
50
    /**
51
     * @param string $key
52
     * @param UploadedFile $file
53
     * @throws RuntimeException
54
     */
55
    protected function uploadToFlysystem(string $key, UploadedFile $file): void
56
    {
57
        if (!$this->filesystem->writeStream($key, fopen($file->getPathname(), 'r'))) {
58
            throw new RuntimeException('Failed to upload to flysystem');
59
        }
60
    }
61
}
62