FixtureFileUploader   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 12
dl 0
loc 45
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A upload() 0 11 2
A remove() 0 7 2
A __construct() 0 3 1
1
<?php
2
3
/*
4
 * This file is part of Monsieur Biz' Rich Editor plugin for Sylius.
5
 *
6
 * (c) Monsieur Biz <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace MonsieurBiz\SyliusRichEditorPlugin\Uploader;
15
16
use Gaufrette\FilesystemInterface;
17
use Symfony\Component\HttpFoundation\File\File;
18
use Webmozart\Assert\Assert;
19
20
final class FixtureFileUploader implements FixtureFileUploaderInterface
21
{
22
    /**
23
     * @var FilesystemInterface
24
     */
25
    private FilesystemInterface $filesystem;
26
27
    /**
28
     * @param FilesystemInterface $filesystem
29
     */
30
    public function __construct(FilesystemInterface $filesystem)
31
    {
32
        $this->filesystem = $filesystem;
33
    }
34
35
    /**
36
     * @param File $file
37
     * @param string $target
38
     */
39
    public function upload(File $file, string $target): void
40
    {
41
        Assert::isInstanceOf($file, File::class);
42
43
        if ($this->filesystem->has($target)) {
44
            $this->remove($target);
45
        }
46
47
        $this->filesystem->write(
48
            $target,
49
            (string) file_get_contents($file->getPathname())
50
        );
51
    }
52
53
    /**
54
     * @param string $path
55
     *
56
     * @return bool
57
     */
58
    public function remove(string $path): bool
59
    {
60
        if ($this->filesystem->has($path)) {
61
            return $this->filesystem->delete($path);
62
        }
63
64
        return false;
65
    }
66
}
67