1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* For licensing terms, see /license.txt */ |
6
|
|
|
|
7
|
|
|
namespace Chamilo\CoreBundle\DataPersister; |
8
|
|
|
|
9
|
|
|
use ApiPlatform\Core\DataPersister\ContextAwareDataPersisterInterface; |
10
|
|
|
use Chamilo\CoreBundle\Entity\UserRelUser; |
11
|
|
|
use Doctrine\ORM\EntityManager; |
12
|
|
|
|
13
|
|
|
class UserRelUserDataPersister |
14
|
|
|
{ |
15
|
|
|
private EntityManager $entityManager; |
16
|
|
|
private ContextAwareDataPersisterInterface $decorated; |
17
|
|
|
|
18
|
|
|
public function __construct(ContextAwareDataPersisterInterface $decorated, EntityManager $entityManager) |
19
|
|
|
{ |
20
|
|
|
$this->decorated = $decorated; |
21
|
|
|
$this->entityManager = $entityManager; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function supports($data, array $context = []): bool |
25
|
|
|
{ |
26
|
|
|
return $this->decorated->supports($data, $context); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function persist($data, array $context = []) |
30
|
|
|
{ |
31
|
|
|
$result = $this->decorated->persist($data, $context); |
32
|
|
|
|
33
|
|
|
if ( |
34
|
|
|
$data instanceof UserRelUser && ( |
35
|
|
|
//($context['collection_operation_name'] ?? null) === 'post' || |
36
|
|
|
//($context['graphql_operation_name'] ?? null) === 'create' |
37
|
|
|
($context['item_operation_name'] ?? null) === 'put' // on update |
38
|
|
|
) |
39
|
|
|
) { |
40
|
|
|
if (UserRelUser::USER_RELATION_TYPE_FRIEND === $data->getRelationType()) { |
41
|
|
|
//error_log((string)$data->getRelationType()); |
42
|
|
|
$repo = $this->entityManager->getRepository(UserRelUser::class); |
43
|
|
|
// Check if the inverse connection is a friend request. |
44
|
|
|
$connection = $repo->findOneBy( |
45
|
|
|
[ |
46
|
|
|
'user' => $data->getFriend(), |
47
|
|
|
'friend' => $data->getUser(), |
48
|
|
|
'relationType' => UserRelUser::USER_RELATION_TYPE_FRIEND, |
49
|
|
|
] |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
if (null === $connection) { |
53
|
|
|
$connection = (new UserRelUser()) |
54
|
|
|
->setUser($data->getFriend()) |
55
|
|
|
->setFriend($data->getUser()) |
56
|
|
|
->setRelationType(UserRelUser::USER_RELATION_TYPE_FRIEND) |
57
|
|
|
; |
58
|
|
|
$this->entityManager->persist($connection); |
59
|
|
|
$this->entityManager->flush(); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// call persistence layer to save $data |
65
|
|
|
return $result; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function remove($data, array $context = []): void |
69
|
|
|
{ |
70
|
|
|
// call your persistence layer to delete $data |
71
|
|
|
$this->entityManager->remove($data); |
72
|
|
|
$this->entityManager->flush(); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|