Passed
Push — master ( 4e7d2f...7a7af2 )
by Julito
08:07
created

AssetDirectoryNamer::directoryName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 13
rs 10
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CoreBundle\Component\Utils;
6
7
use Symfony\Component\PropertyAccess\PropertyAccess;
8
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
9
use Vich\UploaderBundle\Mapping\PropertyMapping;
10
use Vich\UploaderBundle\Naming\ConfigurableInterface;
11
use Vich\UploaderBundle\Naming\DirectoryNamerInterface;
12
use Vich\UploaderBundle\Util\Transliterator;
13
14
class AssetDirectoryNamer implements DirectoryNamerInterface, ConfigurableInterface
15
{
16
    /**
17
     * @var string
18
     */
19
    private $propertyPath;
20
    private $charsPerDir = 2;
21
    private $dirs = 1;
22
23
    /**
24
     * @var bool
25
     */
26
    private $transliterate = false;
0 ignored issues
show
introduced by
The private property $transliterate is not used, and could be removed.
Loading history...
27
28
    /**
29
     * @var PropertyAccessorInterface
30
     */
31
    protected $propertyAccessor;
32
33
    /**
34
     * @var Transliterator
35
     */
36
    private $transliterator;
37
38
    public function __construct(?PropertyAccessorInterface $propertyAccessor, Transliterator $transliterator)
39
    {
40
        $this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
41
        $this->transliterator = $transliterator;
42
    }
43
44
    /**
45
     * @param array $options Options for this namer. The following options are accepted:
46
     *                       - chars_per_dir: how many chars use for each dir.
47
     *                       - dirs: how many dirs create
48
     */
49
    public function configure(array $options): void
50
    {
51
        if (empty($options['property'])) {
52
            throw new \InvalidArgumentException('Option "property" is missing or empty.');
53
        }
54
55
        $this->propertyPath = $options['property'];
56
57
        $options = \array_merge(['chars_per_dir' => $this->charsPerDir, 'dirs' => $this->dirs], $options);
58
59
        $this->charsPerDir = $options['chars_per_dir'];
60
        $this->dirs = $options['dirs'];
61
    }
62
63
    public function directoryName($object, PropertyMapping $mapping): string
64
    {
65
        $fileName = $mapping->getFileName($object);
66
67
        $category = $this->propertyAccessor->getValue($object, $this->propertyPath);
68
69
        $parts[] = $category;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$parts was never initialized. Although not strictly required by PHP, it is generally a good practice to add $parts = array(); before regardless.
Loading history...
70
        /*for ($i = 0, $start = 0; $i < $this->dirs; $i++, $start += $this->charsPerDir) {
71
            $parts[] = \substr($fileName, $start, $this->charsPerDir);
72
        }*/
73
        $parts[] = $fileName;
74
75
        return \implode('/', $parts);
76
    }
77
}
78