Completed
Push — php71-support ( 2570a5...950570 )
by Kamil
30:36
created

TaxonSlugController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 3
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\Bundle\TaxonomyBundle\Controller;
13
14
use Sylius\Component\Resource\Factory\FactoryInterface;
15
use Sylius\Component\Resource\Repository\RepositoryInterface;
16
use Sylius\Component\Taxonomy\Generator\TaxonSlugGeneratorInterface;
17
use Sylius\Component\Taxonomy\Model\TaxonInterface;
18
use Symfony\Component\HttpFoundation\JsonResponse;
19
use Symfony\Component\HttpFoundation\Request;
20
21
final class TaxonSlugController
22
{
23
    /** @var TaxonSlugGeneratorInterface */
24
    private $taxonSlugGenerator;
25
26
    /** @var RepositoryInterface */
27
    private $taxonRepository;
28
29
    /** @var FactoryInterface */
30
    private $taxonFactory;
31
32
    public function __construct(
33
        TaxonSlugGeneratorInterface $taxonSlugGenerator,
34
        RepositoryInterface $taxonRepository,
35
        FactoryInterface $taxonFactory
36
    ) {
37
        $this->taxonSlugGenerator = $taxonSlugGenerator;
38
        $this->taxonRepository = $taxonRepository;
39
        $this->taxonFactory = $taxonFactory;
40
    }
41
42
    /**
43
     * @param Request $request
44
     *
45
     * @return JsonResponse
46
     */
47
    public function generateAction(Request $request)
48
    {
49
        $name = $request->query->get('name');
50
        $locale = $request->query->get('locale');
51
        $parentId = $request->query->get('parentId');
52
53
        /** @var TaxonInterface $taxon */
54
        $taxon = $this->taxonFactory->createNew();
55
        $taxon->setCurrentLocale($locale);
56
        $taxon->setFallbackLocale($locale);
57
        $taxon->setName($name);
58
59
        if (null !== $parentId) {
60
            $taxon->setParent($this->taxonRepository->find($parentId));
61
        }
62
63
        return new JsonResponse([
64
            'slug' => $this->taxonSlugGenerator->generate($taxon, $locale),
65
        ]);
66
    }
67
}
68