|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* For licensing terms, see /license.txt */ |
|
4
|
|
|
|
|
5
|
|
|
declare(strict_types=1); |
|
6
|
|
|
|
|
7
|
|
|
namespace Chamilo\CoreBundle\State; |
|
8
|
|
|
|
|
9
|
|
|
use ApiPlatform\Metadata\DeleteOperationInterface; |
|
10
|
|
|
use ApiPlatform\Metadata\Operation; |
|
11
|
|
|
use ApiPlatform\Metadata\Put; |
|
12
|
|
|
use ApiPlatform\State\ProcessorInterface; |
|
13
|
|
|
use Chamilo\CoreBundle\Entity\UserRelUser; |
|
14
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
15
|
|
|
|
|
16
|
|
|
final class UserRelUserStateProcessor implements ProcessorInterface |
|
17
|
|
|
{ |
|
18
|
|
|
public function __construct( |
|
19
|
|
|
private readonly ProcessorInterface $persistProcessor, |
|
20
|
|
|
private readonly ProcessorInterface $removeProcessor, |
|
21
|
|
|
private readonly EntityManagerInterface $entityManager, |
|
22
|
|
|
) {} |
|
23
|
|
|
|
|
24
|
|
|
public function process($data, Operation $operation, array $uriVariables = [], array $context = []): UserRelUser |
|
25
|
|
|
{ |
|
26
|
|
|
if ($operation instanceof DeleteOperationInterface) { |
|
27
|
|
|
return $this->removeProcessor->process($data, $operation, $uriVariables, $context); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
$result = $this->persistProcessor->process($data, $operation, $uriVariables, $context); |
|
31
|
|
|
|
|
32
|
|
|
assert($result instanceof UserRelUser); |
|
33
|
|
|
|
|
34
|
|
|
if ($operation instanceof Put) { |
|
35
|
|
|
if (UserRelUser::USER_RELATION_TYPE_FRIEND === $data->getRelationType()) { |
|
36
|
|
|
$repo = $this->entityManager->getRepository(UserRelUser::class); |
|
37
|
|
|
// Check if the inverse connection is a friend request. |
|
38
|
|
|
$connection = $repo->findOneBy( |
|
39
|
|
|
[ |
|
40
|
|
|
'user' => $data->getFriend(), |
|
41
|
|
|
'friend' => $data->getUser(), |
|
42
|
|
|
'relationType' => UserRelUser::USER_RELATION_TYPE_FRIEND, |
|
43
|
|
|
] |
|
44
|
|
|
); |
|
45
|
|
|
|
|
46
|
|
|
if (null === $connection) { |
|
47
|
|
|
$connection = (new UserRelUser()) |
|
48
|
|
|
->setUser($data->getFriend()) |
|
49
|
|
|
->setFriend($data->getUser()) |
|
50
|
|
|
->setRelationType(UserRelUser::USER_RELATION_TYPE_FRIEND) |
|
51
|
|
|
; |
|
52
|
|
|
$this->entityManager->persist($connection); |
|
53
|
|
|
$this->entityManager->flush(); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $result; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|