Completed
Pull Request — master (#30)
by
unknown
07:39
created

BlockController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 1
eloc 11
nc 1
nop 5
1
<?php
2
3
namespace SumoCoders\FrameworkMultiUserBundle\Controller;
4
5
use SumoCoders\FrameworkMultiUserBundle\User\Interfaces\UserRepository;
6
use Symfony\Component\HttpFoundation\RedirectResponse;
7
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
8
use Symfony\Component\Routing\Router;
9
use Symfony\Component\Translation\TranslatorInterface;
10
11
final class BlockController
12
{
13
    /** @var UserRepository */
14
    private $userRepository;
15
16
    /** @var FlashBagInterface */
17
    private $flashBag;
18
19
    /** @var TranslatorInterface */
20
    private $translator;
21
22
    /** @var Router */
23
    private $router;
24
25
    /** @var string */
26
    private $redirectRoute;
27
28
    /**
29
     * @param UserRepository $userRepository
30
     * @param FlashBagInterface $flashBag
31
     * @param TranslatorInterface $translator
32
     * @param Router $router
33
     * @param string $redirectRoute
34
     */
35
    public function __construct(
36
        UserRepository $userRepository,
37
        FlashBagInterface $flashBag,
38
        TranslatorInterface $translator,
39
        Router $router,
40
        $redirectRoute
41
    ) {
42
        $this->userRepository = $userRepository;
43
        $this->flashBag = $flashBag;
44
        $this->translator = $translator;
45
        $this->router = $router;
46
        $this->redirectRoute = $redirectRoute;
47
    }
48
49
    /**
50
     * @param int $id
51
     *
52
     * @return RedirectResponse
53
     */
54
    public function toggleAction($id)
55
    {
56
        $user = $this->userRepository->find($id);
57
        $user->toggleBlock();
58
        $this->userRepository->save($user);
59
60
        if ($this->redirectRoute !== null) {
61
            $this->flashBag->add(
62
                'success',
63
                $this->translator->trans(
64
                    'sumocoders.multiuserbundle.flash.' . ($user->isBlocked() ? 'block_success' : 'unblock_success')
65
                )
66
            );
67
68
            return new RedirectResponse($this->router->generate($this->redirectRoute));
69
        }
70
71
        return new RedirectResponse('/');
72
    }
73
}
74