Passed
Push — master ( 5bdcad...49c220 )
by Dejan
02:59
created

FriendshipRequestsController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 94
Duplicated Lines 69.15 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
dl 65
loc 94
rs 10
c 3
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A sendFriendshipRequestAction() 0 11 1
A declineFriendshipRequestAction() 20 20 2
A acceptFriendshipRequestAction() 20 20 2
A cancelFriendshipRequestAction() 23 23 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * Donut Social Network - Yet another experimental social network.
5
 * Copyright (C) 2016-2017, 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-2017, 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\Exceptions\ResourceNotFoundException;
31
use Angelov\Donut\Friendships\FriendshipRequests\Commands\AcceptFriendshipRequestCommand;
32
use Angelov\Donut\Friendships\FriendshipRequests\Commands\CancelFriendshipRequestCommand;
33
use Angelov\Donut\Friendships\FriendshipRequests\Commands\DeclineFriendshipRequestCommand;
34
use Angelov\Donut\Friendships\FriendshipRequests\Commands\SendFriendshipRequestCommand;
35
use Angelov\Donut\Friendships\FriendshipRequests\FriendshipRequest;
36
use Angelov\Donut\Users\User;
37
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
38
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
39
use Symfony\Component\HttpFoundation\Response;
40
41
class FriendshipRequestsController extends Controller
42
{
43
    /**
44
     * @Route("/friendships/send/{id}", name="friendships.requests.store", methods={"GET"})
45
     */
46
    public function sendFriendshipRequestAction(User $user) : Response
47
    {
48
        $currentUser = $this->getUser();
49
50
        $id = $this->get('app.core.uuid_generator')->generate();
51
52
        $this->get('app.core.command_bus.default')->handle(new SendFriendshipRequestCommand($id, $currentUser->getId(), $user->getId()));
53
54
        $this->addFlash('success', 'Friendship request successfully sent!');
55
56
        return $this->redirectToRoute('app.friends.index');
57
    }
58
59
    /**
60
     * @Route("/friendships/cancel/{id}", name="friendships.requests.cancel", methods={"GET"})
61
     */
62 View Code Duplication
    public function cancelFriendshipRequestAction(User $user) : Response
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
    {
64
        // @todo use a voter to check if the user can cancel the request
65
66
        $em = $this->getDoctrine()->getManager();
67
        $repository = $em->getRepository(FriendshipRequest::class);
68
69
        /** @var FriendshipRequest $friendshipRequest */
70
        $friendshipRequest = $repository->findOneBy([
71
            'fromUser' => $this->getUser(),
72
            'toUser' => $user
73
        ]);
74
75
        try {
76
            $this->get('app.core.command_bus.default')->handle(new CancelFriendshipRequestCommand($friendshipRequest->getId()));
77
        } catch (ResourceNotFoundException $e) {
78
            $this->addFlash('error', 'Something went wrong!');
79
            return $this->redirectToRoute('app.friends.index');
80
        }
81
82
        $this->addFlash('success', 'Friendship request successfully cancelled!');
83
84
        return $this->redirectToRoute('app.friends.index');
85
    }
86
87
    /**
88
     * @Route("/friendships/decline/{id}", name="friendships.requests.decline", methods={"GET"})
89
     */
90 View Code Duplication
    public function declineFriendshipRequestAction(User $user) : Response
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
    {
92
        $em = $this->getDoctrine()->getManager();
93
        $repository = $em->getRepository(FriendshipRequest::class);
94
95
        $friendshipRequest = $repository->findOneBy([
96
            'fromUser' => $user,
97
            'toUser' => $this->getUser()
98
        ]);
99
100
        try {
101
            $this->get('app.core.command_bus.default')->handle(new DeclineFriendshipRequestCommand($friendshipRequest->getId()));
102
        } catch (ResourceNotFoundException $e) {
103
            $this->addFlash('error', 'Something went wrong!');
104
            return $this->redirectToRoute('app.friends.index');
105
        }
106
107
        $this->addFlash('success', 'Friendship request successfully declined!');
108
109
        return $this->redirectToRoute('app.friends.index');
110
    }
111
112
    /**
113
     * @Route("/friendships/{id}", name="friendships.requests.accept")
114
     */
115 View Code Duplication
    public function acceptFriendshipRequestAction(User $user) : Response
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
    {
117
        $em = $this->getDoctrine()->getManager();
118
        $repository = $em->getRepository(FriendshipRequest::class);
119
120
        $friendshipRequest = $repository->findOneBy([
121
            'fromUser' => $user,
122
            'toUser' => $this->getUser()
123
        ]);
124
125
        try {
126
            $this->get('app.core.command_bus.default')->handle(new AcceptFriendshipRequestCommand($friendshipRequest->getId()));
127
        } catch (ResourceNotFoundException $e) {
128
            $this->addFlash('error', 'Something went wrong!');
129
            return $this->redirectToRoute('app.friends.index');
130
        }
131
132
        $this->addFlash('success', 'Friendship request successfully accepted!');
133
134
        return $this->redirectToRoute('app.friends.index');
135
    }
136
}
137