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

UploadedImagePathGenerator   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 17
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 1
dl 0
loc 17
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A generate() 0 9 1
A expandPath() 0 4 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