Code Duplication    Length = 21-24 lines in 4 locations

src/AppBundle/Controller/FriendshipRequestsController.php 3 locations

@@ 62-85 (lines=24) @@
59
    /**
60
     * @Route("/friendships/cancel/{id}", name="friendships.requests.cancel", methods={"GET"})
61
     */
62
    public function cancelFriendshipRequestAction(User $user) : Response
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"})
@@ 90-110 (lines=21) @@
87
    /**
88
     * @Route("/friendships/decline/{id}", name="friendships.requests.decline", methods={"GET"})
89
     */
90
    public function declineFriendshipRequestAction(User $user) : Response
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")
@@ 115-135 (lines=21) @@
112
    /**
113
     * @Route("/friendships/{id}", name="friendships.requests.accept")
114
     */
115
    public function acceptFriendshipRequestAction(User $user) : Response
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

src/AppBundle/Controller/FriendshipsController.php 1 location

@@ 43-65 (lines=23) @@
40
     * @Route("/friendships/remove/{id}", name="app.friendships.remove", methods={"GET"})
41
     * @todo fix to use delete requests
42
     */
43
    public function delete(User $user) : Response
44
    {
45
        // @todo use custom repository
46
        $em = $this->get('doctrine.orm.entity_manager');
47
        $repository = $em->getRepository(Friendship::class);
48
49
        if (! $user->isFriendWith($this->getUser())) {
50
            $this->addFlash('error', 'Can\'t broke a nonexisting friendship.');
51
52
            return $this->redirectToRoute('app.friends.index');
53
        }
54
55
        $friendship = $repository->findOneBy([
56
            'user' => $this->getUser(),
57
            'friend' => $user
58
        ]);
59
60
        $this->get('app.core.command_bus.default')->handle(new DeleteFriendshipCommand($friendship->getId()));
61
62
        $this->addFlash('success', 'Sorry to see broken friendships.');
63
64
        return $this->redirectToRoute('app.friends.index');
65
    }
66
}
67