Completed
Push — master ( 229510...0875a1 )
by
unknown
9s
created

UserController::getFormForId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
3
namespace SumoCoders\FrameworkMultiUserBundle\Controller;
4
5
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
6
use SumoCoders\FrameworkMultiUserBundle\Command\Handler;
7
use SumoCoders\FrameworkMultiUserBundle\User\UserRepository;
8
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
use Symfony\Component\Form\Form;
11
use Symfony\Component\Form\FormTypeInterface;
12
use Symfony\Component\HttpFoundation\Request;
13
14
/**
15
 * This class handles all the user actions.
16
 * Register a service for each action.
17
 */
18
class UserController extends Controller
19
{
20
    /**
21
     * @var Handler
22
     */
23
    private $handler;
24
25
    /**
26
     * @var FormTypeInterface
27
     */
28
    private $form;
29
30
    /**
31
     * @var UserRepository
32
     */
33
    private $userRepository;
34
35
    /**
36
     * @var string
37
     */
38
    private $redirectRoute;
39
40
    /**
41
     * @param ContainerInterface $container
42
     * @param FormTypeInterface $form
43
     * @param Handler $handler
44
     * @param UserRepository $userRepository
45
     * @param string $redirectRoute = null
46
     */
47
    public function __construct(
48
        ContainerInterface $container,
49
        FormTypeInterface $form,
50
        Handler $handler,
51
        UserRepository $userRepository,
52
        $redirectRoute = null
53
    ) {
54
        $this->form = $form;
55
        $this->handler = $handler;
56
        $this->userRepository = $userRepository;
57
        $this->redirectRoute = $redirectRoute;
58
        $this->setContainer($container);
59
    }
60
61
    /**
62
     * @param Request $request
63
     * @param int $id
64
     *
65
     * @Template()
66
     *
67
     * @return array
68
     */
69
    public function baseAction(Request $request, $id = null)
70
    {
71
        $form = $this->getFormForId($id);
72
        $form->handleRequest($request);
73
74
        if ($form->isSubmitted() && $form->isValid()) {
75
            $command = $form->getData();
76
            $this->handler->handle($command);
77
78
            if ($this->redirectRoute !== null) {
79
                return $this->redirect($this->redirectRoute);
80
            }
81
        }
82
83
        return ['form' => $form->createView()];
84
    }
85
86
    /**
87
     * @param int $id = null
88
     *
89
     * @return Form
90
     */
91
    private function getFormForId($id = null)
92
    {
93
        if ($id === null) {
94
            return $this->createForm($this->form);
95
        }
96
97
        $user = $this->userRepository->find((int) $id);
98
        $dataTransferObjectClass = $this->form->getDataTransferObjectClass();
99
        $dataTransferObject = $dataTransferObjectClass::fromUser($user);
100
101
        return $this->createForm($this->form, $dataTransferObject);
102
    }
103
}
104