Completed
Push — master ( 4f34c8...2960e8 )
by Kamil
23:03 queued 15:59
created

ImageUploader::expandPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
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 Sylius\Component\Core\Uploader;
15
16
use Gaufrette\Filesystem;
17
use Sylius\Component\Core\Generator\ImagePathGeneratorInterface;
18
use Sylius\Component\Core\Generator\UploadedImagePathGenerator;
19
use Sylius\Component\Core\Model\ImageInterface;
20
use Symfony\Component\HttpFoundation\File\File;
21
use Webmozart\Assert\Assert;
22
23
class ImageUploader implements ImageUploaderInterface
24
{
25
    /** @var Filesystem */
26
    protected $filesystem;
27
28
    /** @var ImagePathGeneratorInterface */
29
    protected $imagePathGenerator;
30
31
    public function __construct(
32
        Filesystem $filesystem,
33
        ?ImagePathGeneratorInterface $imagePathGenerator = null
34
    ) {
35
        $this->filesystem = $filesystem;
36
37
        if ($imagePathGenerator === null) {
38
            @trigger_error(sprintf(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

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...
39
                'Not passing an $imagePathGenerator to %s constructor is deprecated since Sylius 1.6 and will be not possible in Sylius 2.0.', self::class
40
            ), \E_USER_DEPRECATED);
41
        }
42
43
        $this->imagePathGenerator = $imagePathGenerator ?? new UploadedImagePathGenerator();
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function upload(ImageInterface $image): void
50
    {
51
        if (!$image->hasFile()) {
52
            return;
53
        }
54
55
        /** @var File $file */
56
        $file = $image->getFile();
57
58
        Assert::isInstanceOf($file, File::class);
59
60
        if (null !== $image->getPath() && $this->has($image->getPath())) {
61
            $this->remove($image->getPath());
62
        }
63
64
        do {
65
            $path = $this->imagePathGenerator->generate($image);
66
        } while ($this->isAdBlockingProne($path) || $this->filesystem->has($path));
67
68
        $image->setPath($path);
69
70
        $this->filesystem->write(
71
            $image->getPath(),
72
            file_get_contents($image->getFile()->getPathname())
73
        );
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function remove(string $path): bool
80
    {
81
        if ($this->filesystem->has($path)) {
82
            return $this->filesystem->delete($path);
83
        }
84
85
        return false;
86
    }
87
88
    private function has(string $path): bool
89
    {
90
        return $this->filesystem->has($path);
91
    }
92
93
    /**
94
     * Will return true if the path is prone to be blocked by ad blockers
95
     */
96
    private function isAdBlockingProne(string $path): bool
97
    {
98
        return strpos($path, 'ad') !== false;
99
    }
100
}
101