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