Completed
Push — master ( 1f36be...d8b059 )
by Beñat
03:59
created

RemoveTranslationActionType::execute()   B

Complexity

Conditions 3
Paths 7

Size

Total Lines 41
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 27
nc 7
nop 4
1
<?php
2
3
/*
4
 * This file is part of the CMS Kernel library.
5
 *
6
 * Copyright (c) 2016 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\Action\ActionType;
16
use LIN3S\AdminBundle\Action\Type\EntityId;
17
use LIN3S\AdminBundle\Configuration\EntityConfiguration;
18
use Symfony\Component\HttpFoundation\RedirectResponse;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\HttpFoundation\Session\Session;
21
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
22
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
23
24
/**
25
 * @author Beñat Espiña <[email protected]>
26
 */
27
class RemoveTranslationActionType implements ActionType
28
{
29
    use EntityId;
30
31
    private $manager;
32
    private $session;
33
    private $twig;
34
    private $urlGenerator;
35
36
    public function __construct(
37
        UrlGeneratorInterface $urlGenerator,
38
        ObjectManager $manager,
39
        \Twig_Environment $twig,
40
        Session $session
41
    ) {
42
        $this->manager = $manager;
43
        $this->twig = $twig;
44
        $this->session = $session;
45
        $this->urlGenerator = $urlGenerator;
46
    }
47
48
    public function execute($entity, EntityConfiguration $config, Request $request, $options = null)
49
    {
50
        $id = (string) $this->getEntityId($entity, $config);
51
        if (!$entity) {
52
            throw new NotFoundHttpException(
53
                sprintf('The translatable with %s id does not exist', $id)
54
            );
55
        }
56
57
        $locale = $request->query->get('locale');
58
        try {
59
            $translation = $entity->{$locale}();
60
            $entity->removeTranslation($translation->locale());
61
            $this->manager->persist($entity);
62
            $this->manager->flush();
63
64
            $this->session->getFlashBag()->add(
65
                'lin3s_admin_success',
66
                sprintf(
67
                    'The %s translation of %s is successfully removed',
68
                    $locale,
69
                    $config->name()
70
                )
71
            );
72
        } catch (\Exception $exception) {
73
            $this->session->getFlashBag()->add(
74
                'lin3s_admin_error',
75
                sprintf(
76
                    'Errors while remove the %s translation of %s',
77
                    $locale,
78
                    $config->name()
79
                )
80
            );
81
        }
82
83
        return new RedirectResponse(
84
            $this->urlGenerator->generate('lin3s_admin_list', [
85
                'entity' => $config->name(),
86
            ])
87
        );
88
    }
89
}
90