ThoughtsController   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 26
c 3
b 0
f 0
dl 0
loc 60
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A delete() 0 11 2
A indexAction() 0 30 3
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\Core\ResultLists\Sorting\OrderDirection;
32
use Angelov\Donut\Core\ResultLists\Sorting\OrderField;
33
use Angelov\Donut\Thoughts\ThoughtsFeed\ThoughtsFeedInterface;
34
use Angelov\Donut\Thoughts\Commands\DeleteThoughtCommand;
35
use Angelov\Donut\Thoughts\Commands\StoreThoughtCommand;
36
use Angelov\Donut\Thoughts\Thought;
37
use Angelov\Donut\Thoughts\Form\ThoughtType;
38
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
39
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
40
use Symfony\Component\HttpFoundation\Request;
41
use Symfony\Component\HttpFoundation\Response;
42
43
class ThoughtsController extends AbstractController
44
{
45
    private $commandBus;
46
47
    public function __construct(CommandBusInterface $commandBus)
48
    {
49
        $this->commandBus = $commandBus;
50
    }
51
52
    /**
53
     * @Route("/thoughts", name="app.thoughts.index", methods={"GET", "HEAD", "POST"})
54
     */
55
    public function indexAction(Request $request, ThoughtsFeedInterface $thoughtsList) : Response
56
    {
57
        $form = $this->createForm(ThoughtType::class);
58
        $form->handleRequest($request);
59
60
        if ($form->isSubmitted() && $form->isValid()) {
61
62
            /** @var StoreThoughtCommand $thought */
63
            $command = $form->getData();
64
65
            $this->commandBus->handle($command);
66
67
            $this->addFlash('success', 'Thought shared!');
68
69
            return $this->redirectToRoute('app.thoughts.index');
70
        }
71
72
        $page = $request->query->get('page', 1);
73
        $perPage = 10;
74
        $offset = ($page-1)*$perPage;
75
76
        $thoughtsList->filterSource(ThoughtsFeedInterface::FROM_FRIENDS);
77
        $thoughtsList->includeOwnThoughts();
78
        $thoughtsList->orderBy([new OrderField('thought.createdAt', OrderDirection::DESC)]);
79
        $thoughtsList->setItemsPerPage($perPage);
80
        $thoughtsList->setOffset($offset);
81
82
        return $this->render('default/index.html.twig', [
83
            'form' => $form->createView(),
84
            'thoughts_list' => $thoughtsList
85
        ]);
86
    }
87
88
    /**
89
     * @Route("/thoughts/{id}", name="app.thoughts.delete", methods={"POST"})
90
     * @todo change to use DELETE method
91
     */
92
    public function delete(Thought $thought) : Response
93
    {
94
        if (!$this->isGranted('DELETE_THOUGHT', $thought)) {
95
            return $this->redirectToRoute('app.thoughts.index');
96
        }
97
98
        $this->commandBus->handle(new DeleteThoughtCommand($thought->getId()));
99
100
        $this->addFlash('success', 'Thought deleted!');
101
102
        return $this->redirectToRoute('app.thoughts.index');
103
    }
104
}
105