Completed
Pull Request — master (#13)
by
unknown
18:53 queued 09:10
created

UserController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
c 1
b 0
f 1
lcom 1
cbo 3
dl 0
loc 88
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A baseAction() 0 16 4
A getFormForId() 0 12 2
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
     * UserController constructor.
42
     *
43
     * @param ContainerInterface $container
44
     * @param FormTypeInterface $form
45
     * @param Handler $handler
46
     * @param UserRepository $userRepository
47
     * @param string $redirectRoute
48
     */
49
    public function __construct(
50
        ContainerInterface $container,
51
        FormTypeInterface $form,
52
        Handler $handler,
53
        UserRepository $userRepository,
54
        $redirectRoute = null
55
    ) {
56
        $this->form = $form;
57
        $this->handler = $handler;
58
        $this->userRepository = $userRepository;
59
        $this->redirectRoute = $redirectRoute;
60
        $this->setContainer($container);
61
    }
62
63
    /**
64
     * @param Request $request
65
     * @param int $id
66
     *
67
     * @Template()
68
     *
69
     * @return array
70
     */
71
    public function baseAction(Request $request, $id = null)
72
    {
73
        $form = $this->getFormForId((int) $id);
74
        $form->handleRequest($request);
75
76
        if ($form->isSubmitted() && $form->isValid()) {
77
            $command = $form->getData();
78
            $this->handler->handle($command);
79
80
            if ($this->redirectRoute !== null) {
81
                return $this->redirect($this->redirectRoute);
82
            }
83
        }
84
85
        return ['form' => $form->createView()];
86
    }
87
88
    /**
89
     * @param int $id = null
90
     *
91
     * @return Form
92
     */
93
    private function getFormForId($id = null)
94
    {
95
        if ($id === null) {
96
            return $this->createForm($this->form);
97
        }
98
99
        $user = $this->userRepository->find($id);
100
        $dataTransferObjectClass = $this->form->getDataTransferObjectClass();
101
        $dataTransferObject = $dataTransferObjectClass::fromUser($user);
102
103
        return $this->createForm($this->form, $dataTransferObject);
104
    }
105
}
106