|
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\Exceptions\ResourceNotFoundException; |
|
32
|
|
|
use Angelov\Donut\Core\UuidGenerator\UuidGeneratorInterface; |
|
33
|
|
|
use Angelov\Donut\Friendships\FriendshipRequests\Commands\AcceptFriendshipRequestCommand; |
|
34
|
|
|
use Angelov\Donut\Friendships\FriendshipRequests\Commands\CancelFriendshipRequestCommand; |
|
35
|
|
|
use Angelov\Donut\Friendships\FriendshipRequests\Commands\DeclineFriendshipRequestCommand; |
|
36
|
|
|
use Angelov\Donut\Friendships\FriendshipRequests\Commands\SendFriendshipRequestCommand; |
|
37
|
|
|
use Angelov\Donut\Friendships\FriendshipRequests\FriendshipRequest; |
|
38
|
|
|
use Angelov\Donut\Users\User; |
|
39
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
|
40
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
41
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
42
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
|
43
|
|
|
|
|
44
|
|
|
class FriendshipRequestsController extends AbstractController |
|
45
|
|
|
{ |
|
46
|
|
|
private $uuidGenerator; |
|
47
|
|
|
private $commandBus; |
|
48
|
|
|
|
|
49
|
|
|
public function __construct(UuidGeneratorInterface $uuidGenerator, CommandBusInterface $commandBus) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->uuidGenerator = $uuidGenerator; |
|
52
|
|
|
$this->commandBus = $commandBus; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @Route("/friendships/send/{id}", name="friendships.requests.store", methods={"GET"}) |
|
57
|
|
|
*/ |
|
58
|
|
|
public function sendFriendshipRequestAction(User $user, UserInterface $currentUser) : Response |
|
59
|
|
|
{ |
|
60
|
|
|
/** @var $currentUser User */ |
|
61
|
|
|
|
|
62
|
|
|
$id = $this->uuidGenerator->generate(); |
|
63
|
|
|
|
|
64
|
|
|
$this->commandBus->handle(new SendFriendshipRequestCommand($id, $currentUser->getId(), $user->getId())); |
|
65
|
|
|
|
|
66
|
|
|
$this->addFlash('success', 'Friendship request successfully sent!'); |
|
67
|
|
|
|
|
68
|
|
|
return $this->redirectToRoute('app.friends.index'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @Route("/friendships/cancel/{id}", name="friendships.requests.cancel", methods={"GET"}) |
|
73
|
|
|
*/ |
|
74
|
|
|
public function cancelFriendshipRequestAction(User $user) : Response |
|
75
|
|
|
{ |
|
76
|
|
|
// @todo use a voter to check if the user can cancel the request |
|
77
|
|
|
|
|
78
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
79
|
|
|
$repository = $em->getRepository(FriendshipRequest::class); |
|
80
|
|
|
|
|
81
|
|
|
/** @var FriendshipRequest $friendshipRequest */ |
|
82
|
|
|
$friendshipRequest = $repository->findOneBy([ |
|
83
|
|
|
'fromUser' => $this->getUser(), |
|
84
|
|
|
'toUser' => $user |
|
85
|
|
|
]); |
|
86
|
|
|
|
|
87
|
|
|
try { |
|
88
|
|
|
$this->commandBus->handle(new CancelFriendshipRequestCommand($friendshipRequest->getId())); |
|
89
|
|
|
} catch (ResourceNotFoundException $e) { |
|
90
|
|
|
$this->addFlash('error', 'Something went wrong!'); |
|
91
|
|
|
return $this->redirectToRoute('app.friends.index'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$this->addFlash('success', 'Friendship request successfully cancelled!'); |
|
95
|
|
|
|
|
96
|
|
|
return $this->redirectToRoute('app.friends.index'); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @Route("/friendships/decline/{id}", name="friendships.requests.decline", methods={"GET"}) |
|
101
|
|
|
*/ |
|
102
|
|
|
public function declineFriendshipRequestAction(User $user) : Response |
|
103
|
|
|
{ |
|
104
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
105
|
|
|
$repository = $em->getRepository(FriendshipRequest::class); |
|
106
|
|
|
|
|
107
|
|
|
$friendshipRequest = $repository->findOneBy([ |
|
108
|
|
|
'fromUser' => $user, |
|
109
|
|
|
'toUser' => $this->getUser() |
|
110
|
|
|
]); |
|
111
|
|
|
|
|
112
|
|
|
try { |
|
113
|
|
|
$this->commandBus->handle(new DeclineFriendshipRequestCommand($friendshipRequest->getId())); |
|
114
|
|
|
} catch (ResourceNotFoundException $e) { |
|
115
|
|
|
$this->addFlash('error', 'Something went wrong!'); |
|
116
|
|
|
return $this->redirectToRoute('app.friends.index'); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$this->addFlash('success', 'Friendship request successfully declined!'); |
|
120
|
|
|
|
|
121
|
|
|
return $this->redirectToRoute('app.friends.index'); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @Route("/friendships/{id}", name="friendships.requests.accept") |
|
126
|
|
|
*/ |
|
127
|
|
|
public function acceptFriendshipRequestAction(User $user) : Response |
|
128
|
|
|
{ |
|
129
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
130
|
|
|
$repository = $em->getRepository(FriendshipRequest::class); |
|
131
|
|
|
|
|
132
|
|
|
$friendshipRequest = $repository->findOneBy([ |
|
133
|
|
|
'fromUser' => $user, |
|
134
|
|
|
'toUser' => $this->getUser() |
|
135
|
|
|
]); |
|
136
|
|
|
|
|
137
|
|
|
try { |
|
138
|
|
|
$this->commandBus->handle(new AcceptFriendshipRequestCommand($friendshipRequest->getId())); |
|
139
|
|
|
} catch (ResourceNotFoundException $e) { |
|
140
|
|
|
$this->addFlash('error', 'Something went wrong!'); |
|
141
|
|
|
return $this->redirectToRoute('app.friends.index'); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
$this->addFlash('success', 'Friendship request successfully accepted!'); |
|
145
|
|
|
|
|
146
|
|
|
return $this->redirectToRoute('app.friends.index'); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|