Completed
Push — allow-no-default-tax-zone-in-c... ( 67cea0 )
by Kamil
06:23
created

ImageUploader::upload()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.8657
c 0
b 0
f 0
cc 6
nc 3
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\Behat\Service\Uploader;
15
16
use Sylius\Component\Core\Model\ImageInterface;
17
use Sylius\Component\Core\Uploader\ImageUploader as BaseImageUploader;
18
use Sylius\Component\Core\Uploader\ImageUploaderInterface;
19
use Symfony\Component\HttpFoundation\File\File;
20
use Webmozart\Assert\Assert;
21
22
final class ImageUploader extends BaseImageUploader implements ImageUploaderInterface
23
{
24
    public function upload(ImageInterface $image): void
25
    {
26
        if (!$image->hasFile()) {
27
            return;
28
        }
29
30
        $file = $image->getFile();
31
32
        /** @var File $file */
33
        Assert::isInstanceOf($file, File::class);
34
35
        if (null !== $image->getPath() && $this->has($image->getPath())) {
36
            $this->remove($image->getPath());
37
        }
38
39
        do {
40
            $hash = bin2hex(random_bytes(16));
41
            $path = $this->expandPath($hash . '/' . $file->getFilename() . '.' . $file->guessExtension());
42
        } while ($this->isAdBlockingProne($path) || $this->filesystem->has($path));
43
44
        $image->setPath($path);
45
46
        $this->filesystem->write(
47
            $image->getPath(),
48
            file_get_contents($image->getFile()->getPathname())
49
        );
50
    }
51
52
    private function expandPath(string $path): string
0 ignored issues
show
Bug introduced by
Consider using a different method name as you override a private method of the parent class.

Overwriting private methods is generally fine as long as you also use private visibility. It might still be preferable for understandability to use a different method name.

Loading history...
53
    {
54
        return sprintf(
55
            '%s/%s/%s',
56
            substr($path, 0, 2),
57
            substr($path, 2, 2),
58
            substr($path, 4)
59
        );
60
    }
61
62
    private function has(string $path): bool
0 ignored issues
show
Bug introduced by
Consider using a different method name as you override a private method of the parent class.

Overwriting private methods is generally fine as long as you also use private visibility. It might still be preferable for understandability to use a different method name.

Loading history...
63
    {
64
        return $this->filesystem->has($path);
65
    }
66
67
    private function isAdBlockingProne(string $path): bool
0 ignored issues
show
Bug introduced by
Consider using a different method name as you override a private method of the parent class.

Overwriting private methods is generally fine as long as you also use private visibility. It might still be preferable for understandability to use a different method name.

Loading history...
68
    {
69
        return strpos($path, 'ad') !== false;
70
    }
71
}
72