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

CommunitiesController::__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\Communities\Commands\JoinCommunityCommand;
31
use Angelov\Donut\Communities\Commands\LeaveCommunityCommand;
32
use Angelov\Donut\Communities\Commands\StoreCommunityCommand;
33
use Angelov\Donut\Communities\Form\CommunityType;
34
use Angelov\Donut\Communities\Repositories\CommunitiesRepositoryInterface;
35
use Angelov\Donut\Core\CommandBus\CommandBusInterface;
36
use Angelov\Donut\Users\User;
37
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
38
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
39
use Symfony\Component\HttpFoundation\Request;
40
use Symfony\Component\HttpFoundation\Response;
41
use Symfony\Component\Security\Core\User\UserInterface;
42
43
class CommunitiesController extends AbstractController
44
{
45
    private $communities;
46
    private $commandBus;
47
48
    public function __construct(CommunitiesRepositoryInterface $communities, CommandBusInterface $commandBus)
49
    {
50
        $this->communities = $communities;
51
        $this->commandBus = $commandBus;
52
    }
53
54
    /**
55
     * @Route("/communities", name="app.communities.index", methods={"GET", "HEAD"})
56
     */
57
    public function indexAction() : Response
58
    {
59
        $communities = $this->communities->all();
60
61
        return $this->render('communities/index.html.twig', [
62
            'communities' => $communities
63
        ]);
64
    }
65
66
    /**
67
     * @Route("/communities/create", name="app.communities.create")
68
     */
69
    public function createAction(Request $request) : Response
70
    {
71
        $form = $this->createForm(CommunityType::class);
72
        $form->handleRequest($request);
73
74
        if ($form->isSubmitted() && $form->isValid()) {
75
76
            /** @var StoreCommunityCommand $command */
77
            $command = $form->getData();
78
79
            $this->commandBus->handle($command);
80
81
            $this->addFlash('success', 'Community was successfully created!');
82
83
            return $this->redirectToRoute('app.communities.index');
84
        }
85
86
        return $this->render('communities/create.html.twig', [
87
            'form' => $form->createView()
88
        ]);
89
    }
90
91
    /**
92
     * @Route("/communities/{id}", name="app.communities.show", methods={"POST", "GET"})
93
     */
94
    public function showAction($id) : Response
95
    {
96
        $community = $this->communities->find($id);
97
98
        return $this->render('communities/show.html.twig', [
99
            'community' => $community
100
        ]);
101
    }
102
103
    /**
104
     * @Route("/communities/{id}/join", name="app.communities.join", methods={"POST"})
105
     */
106
    public function joinAction(UserInterface $user, $id) : Response
107
    {
108
        /** @var User $user */
109
110
        $this->commandBus->handle(new JoinCommunityCommand($user->getId(), $id));
111
112
        $this->addFlash('success', 'Successfully joined the community');
113
114
        return $this->redirectToRoute('app.communities.index');
115
    }
116
117
    /**
118
     * @Route("/communities/{id}/leave", name="app.communities.leave", methods={"POST"})
119
     */
120
    public function leaveAction(UserInterface $user, $id) : Response
121
    {
122
        /** @var User $user */
123
124
        $this->commandBus->handle(new LeaveCommunityCommand($user->getId(), $id));
125
126
        $this->addFlash('success', 'Successfully left the community');
127
128
        return $this->redirectToRoute('app.communities.index');
129
    }
130
}
131