RemoveTranslationActionType   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 54
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B execute() 0 37 4
1
<?php
2
3
/*
4
 * This file is part of the CMS Kernel package.
5
 *
6
 * Copyright (c) 2016-present LIN3S <[email protected]>
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 LIN3S\CMSKernel\Infrastructure\Lin3sAdminBundle\Action\Type;
13
14
use Doctrine\Common\Persistence\ObjectManager;
15
use LIN3S\AdminBundle\Configuration\Model\Entity;
16
use LIN3S\AdminBundle\Configuration\Type\ActionType;
17
use Symfony\Component\HttpFoundation\RedirectResponse;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
20
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
21
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
22
23
/**
24
 * @author Beñat Espiña <[email protected]>
25
 */
26
class RemoveTranslationActionType implements ActionType
27
{
28
    private $manager;
29
    private $flashBag;
30
    private $urlGenerator;
31
32
    public function __construct(
33
        UrlGeneratorInterface $urlGenerator,
34
        ObjectManager $manager,
35
        FlashBagInterface $flashBag
36
    ) {
37
        $this->manager = $manager;
38
        $this->flashBag = $flashBag;
39
        $this->urlGenerator = $urlGenerator;
40
    }
41
42
    public function execute($entity, Entity $config, Request $request, $options = null)
43
    {
44
        if (!$entity) {
45
            throw new NotFoundHttpException('The translatable does not exist');
46
        }
47
        $entityName = $config->name();
48
        $locale = $request->query->get('locale');
49
50
        try {
51
            $translation = $entity->{$locale}();
52
            $entity->removeTranslation($translation->locale());
53
54
            if ($entity->translations()->count() === 0) {
55
                $this->manager->remove($entity);
56
            } else {
57
                $this->manager->persist($entity);
58
            }
59
60
            $this->manager->flush();
61
62
            $this->flashBag->add(
63
                'lin3s_admin_success',
64
                sprintf('The %s translation of %s is successfully removed', $locale, $entityName)
65
            );
66
        } catch (\Exception $exception) {
67
            $this->flashBag->add(
68
                'lin3s_admin_error',
69
                sprintf('Errors while remove the %s translation of %s', $locale, $entityName)
70
            );
71
        }
72
73
        return new RedirectResponse(
74
            $this->urlGenerator->generate('lin3s_admin_list', [
75
                'entity' => $config->name(),
76
            ])
77
        );
78
    }
79
}
80