IdGenerator   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A generatePath() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\MediaBundle\Generator;
15
16
use Sonata\MediaBundle\Model\MediaInterface;
17
18
/**
19
 * @final since sonata-project/media-bundle 3.21.0
20
 */
21
class IdGenerator implements GeneratorInterface
22
{
23
    /**
24
     * @var int
25
     */
26
    protected $firstLevel;
27
28
    /**
29
     * @var int
30
     */
31
    protected $secondLevel;
32
33
    /**
34
     * @param int $firstLevel
35
     * @param int $secondLevel
36
     */
37
    public function __construct($firstLevel = 100000, $secondLevel = 1000)
38
    {
39
        $this->firstLevel = $firstLevel;
40
        $this->secondLevel = $secondLevel;
41
    }
42
43
    public function generatePath(MediaInterface $media)
44
    {
45
        $rep_first_level = (int) ($media->getId() / $this->firstLevel);
46
        $rep_second_level = (int) (($media->getId() - ($rep_first_level * $this->firstLevel)) / $this->secondLevel);
47
48
        return sprintf('%s/%04s/%02s', $media->getContext(), $rep_first_level + 1, $rep_second_level + 1);
49
    }
50
}
51