Passed
Push — master ( 5b079c...864a4e )
by Dejan
05:31
created

UsersController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Donut Social Network - Yet another experimental social network.
5
 * Copyright (C) 2016-2018, Dejan Angelov <[email protected]>
6
 *
7
 * This file is part of Donut Social Network.
8
 *
9
 * Donut Social Network is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * Donut Social Network is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with Donut Social Network.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 * @package Donut Social Network
23
 * @copyright Copyright (C) 2016-2018, Dejan Angelov <[email protected]>
24
 * @license https://github.com/angelov/donut/blob/master/LICENSE
25
 * @author Dejan Angelov <[email protected]>
26
 */
27
28
namespace AppBundle\Controller;
29
30
use Angelov\Donut\Core\CommandBus\CommandBusInterface;
31
use Angelov\Donut\Users\Commands\StoreUserCommand;
32
use Angelov\Donut\Users\Repositories\UsersRepositoryInterface;
33
use Angelov\Donut\Users\User;
34
use Angelov\Donut\Users\Form\UserRegistrationForm;
35
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
36
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
37
use Symfony\Component\HttpFoundation\Request;
38
use Symfony\Component\HttpFoundation\Response;
39
40
class UsersController extends AbstractController
41
{
42
    private $commandBus;
43
    private $users;
44
45
    public function __construct(CommandBusInterface $commandBus, UsersRepositoryInterface $users)
46
    {
47
        $this->commandBus = $commandBus;
48
        $this->users = $users;
49
    }
50
51
    /**
52
     * @Route("/register", name="app.users.register")
53
     */
54
    public function registerAction(Request $request) : Response
55
    {
56
        $form = $this->createForm(UserRegistrationForm::class);
57
58
        $form->handleRequest($request);
59
60
        if ($form->isSubmitted() && $form->isValid()) {
61
62
            /** @var StoreUserCommand $command */
63
            $command = $form->getData();
64
65
            $this->commandBus->handle($command);
66
67
            $this->addFlash('success', 'Registration was successful. You many now login.');
68
69
            return $this->redirectToRoute('homepage');
70
        }
71
72
        return $this->render('users/register.html.twig', [
73
            'form' => $form->createView()
74
        ]);
75
    }
76
77
    /**
78
     * @Route("/users", name="app.users.index")
79
     */
80
    public function indexAction() : Response
81
    {
82
        $users = $this->users->all();
83
84
        return $this->render('users/index.html.twig', [
85
            'users' => $users
86
        ]);
87
    }
88
89
    /**
90
     * @Route("/users/{id}", name="app.users.show", methods={"GET", "HEAD"})
91
     */
92
    public function showAction(User $user) : Response
93
    {
94
        return $this->render('users/show.html.twig', [
95
            'user' => $user
96
        ]);
97
    }
98
}
99