Completed
Push — remove-content-bundle ( 201341 )
by Kamil
34:43
created

TaxonSlugGenerator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 4
dl 0
loc 34
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A generate() 0 13 2
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
namespace Sylius\Component\Taxonomy\Generator;
13
14
use Behat\Transliterator\Transliterator;
15
use Sylius\Component\Taxonomy\Model\TaxonInterface;
16
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface;
17
use Webmozart\Assert\Assert;
18
19
/**
20
 * @author Mateusz Zalewski <[email protected]>
21
 */
22
final class TaxonSlugGenerator implements TaxonSlugGeneratorInterface
23
{
24
    const SLUG_SEPARATOR = '/';
25
26
    /**
27
     * @var TaxonRepositoryInterface
28
     */
29
    private $taxonRepository;
30
31
    /**
32
     * @param TaxonRepositoryInterface $taxonRepository
33
     */
34
    public function __construct(TaxonRepositoryInterface $taxonRepository)
35
    {
36
        $this->taxonRepository = $taxonRepository;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function generate($name, $parentId = null)
43
    {
44
        $taxonSlug = Transliterator::transliterate($name);
45
        if (null === $parentId) {
46
            return $taxonSlug;
47
        }
48
49
        /** @var TaxonInterface $parent */
50
        $parent = $this->taxonRepository->find($parentId);
51
        Assert::notNull($parent, sprintf('There is no parent taxon with id %d.', $parentId));
52
53
        return $parent->getSlug().self::SLUG_SEPARATOR.$taxonSlug;
54
    }
55
}
56