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

UploadedImagePathGenerator::generate()   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\Generator;
15
16
use Sylius\Component\Core\Model\ImageInterface;
17
use Symfony\Component\HttpFoundation\File\UploadedFile;
18
19
final class UploadedImagePathGenerator implements ImagePathGeneratorInterface
20
{
21
    public function generate(ImageInterface $image): string
22
    {
23
        /** @var UploadedFile $file */
24
        $file = $image->getFile();
25
26
        $hash = bin2hex(random_bytes(16));
27
28
        return $this->expandPath($hash . '.' . $file->guessExtension());
29
    }
30
31
    private function expandPath(string $path): string
32
    {
33
        return sprintf('%s/%s/%s', substr($path, 0, 2), substr($path, 2, 2), substr($path, 4));
34
    }
35
}
36