Internal: API: Replace UserRelUserDataPersister with UserRelUserStateProcessor

pull/5329/head
Angel Fernando Quiroz Campos 7 months ago
parent 450d0ab82a
commit 8bbdaab697
  1. 5
      config/services.yaml
  2. 72
      src/CoreBundle/DataPersister/UserRelUserDataPersister.php
  3. 4
      src/CoreBundle/Entity/UserRelUser.php
  4. 3
      src/CoreBundle/Resources/config/services.yml
  5. 60
      src/CoreBundle/State/UserRelUserStateProcessor.php

@ -86,6 +86,11 @@ services:
bind:
$persistProcessor: '@api_platform.doctrine.orm.state.persist_processor'
Chamilo\CoreBundle\State\UserRelUserStateProcessor:
bind:
$persistProcessor: '@api_platform.doctrine.orm.state.persist_processor'
$removeProcessor: '@api_platform.doctrine.orm.state.remove_processor'
Chamilo\CoreBundle\EventSubscriber\AnonymousUserSubscriber:
tags:
- name: kernel.event_subscriber

@ -1,72 +0,0 @@
<?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\DataPersister;
use ApiPlatform\Core\DataPersister\ContextAwareDataPersisterInterface;
use Chamilo\CoreBundle\Entity\UserRelUser;
use Doctrine\ORM\EntityManager;
class UserRelUserDataPersister implements ContextAwareDataPersisterInterface
{
private EntityManager $entityManager;
private ContextAwareDataPersisterInterface $decorated;
public function __construct(ContextAwareDataPersisterInterface $decorated, EntityManager $entityManager)
{
$this->decorated = $decorated;
$this->entityManager = $entityManager;
}
public function supports($data, array $context = []): bool
{
return $this->decorated->supports($data, $context);
}
public function persist($data, array $context = []): ?object
{
$result = $this->decorated->persist($data, $context);
if ($data instanceof UserRelUser && (
// ($context['collection_operation_name'] ?? null) === 'post' ||
// ($context['graphql_operation_name'] ?? null) === 'create'
($context['item_operation_name'] ?? null) === 'put' // on update
)
) {
if (UserRelUser::USER_RELATION_TYPE_FRIEND === $data->getRelationType()) {
// error_log((string)$data->getRelationType());
$repo = $this->entityManager->getRepository(UserRelUser::class);
// Check if the inverse connection is a friend request.
$connection = $repo->findOneBy(
[
'user' => $data->getFriend(),
'friend' => $data->getUser(),
'relationType' => UserRelUser::USER_RELATION_TYPE_FRIEND,
]
);
if (null === $connection) {
$connection = (new UserRelUser())
->setUser($data->getFriend())
->setFriend($data->getUser())
->setRelationType(UserRelUser::USER_RELATION_TYPE_FRIEND)
;
$this->entityManager->persist($connection);
$this->entityManager->flush();
}
}
}
// call persistence layer to save $data
return $result;
}
public function remove($data, array $context = []): void
{
// call your persistence layer to delete $data
$this->entityManager->remove($data);
$this->entityManager->flush();
}
}

@ -15,6 +15,7 @@ use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use Chamilo\CoreBundle\Entity\Listener\UserRelUserListener;
use Chamilo\CoreBundle\State\UserRelUserStateProcessor;
use Chamilo\CoreBundle\Traits\TimestampableTypedEntity;
use Chamilo\CoreBundle\Traits\UserTrait;
use Doctrine\ORM\Mapping as ORM;
@ -42,7 +43,8 @@ use Symfony\Component\Validator\Constraints as Assert;
denormalizationContext: [
'groups' => ['user_rel_user:write'],
],
security: 'is_granted("ROLE_USER")'
security: 'is_granted("ROLE_USER")',
processor: UserRelUserStateProcessor::class,
)]
#[UniqueEntity(
fields: ['user', 'friend', 'relationType'],

@ -29,9 +29,6 @@ services:
Chamilo\CoreBundle\DataPersister\MessageDataPersister:
arguments: ['@api_platform.doctrine.orm.data_persister']
Chamilo\CoreBundle\DataPersister\UserRelUserDataPersister:
arguments: ['@api_platform.doctrine.orm.data_persister']
# Form types
# Custom yes/no form type, use in the platform settings
Chamilo\CoreBundle\Form\Type\YesNoType:

@ -0,0 +1,60 @@
<?php
/* For licensing terms, see /license.txt */
declare(strict_types=1);
namespace Chamilo\CoreBundle\State;
use ApiPlatform\Metadata\DeleteOperationInterface;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Put;
use ApiPlatform\State\ProcessorInterface;
use Chamilo\CoreBundle\Entity\UserRelUser;
use Doctrine\ORM\EntityManagerInterface;
final class UserRelUserStateProcessor implements ProcessorInterface
{
public function __construct(
private readonly ProcessorInterface $persistProcessor,
private readonly ProcessorInterface $removeProcessor,
private readonly EntityManagerInterface $entityManager,
) {}
public function process($data, Operation $operation, array $uriVariables = [], array $context = []): UserRelUser
{
if ($operation instanceof DeleteOperationInterface) {
return $this->removeProcessor->process($data, $operation, $uriVariables, $context);
}
$result = $this->persistProcessor->process($data, $operation, $uriVariables, $context);
assert($result instanceof UserRelUser);
if ($operation instanceof Put) {
if (UserRelUser::USER_RELATION_TYPE_FRIEND === $data->getRelationType()) {
$repo = $this->entityManager->getRepository(UserRelUser::class);
// Check if the inverse connection is a friend request.
$connection = $repo->findOneBy(
[
'user' => $data->getFriend(),
'friend' => $data->getUser(),
'relationType' => UserRelUser::USER_RELATION_TYPE_FRIEND,
]
);
if (null === $connection) {
$connection = (new UserRelUser())
->setUser($data->getFriend())
->setFriend($data->getUser())
->setRelationType(UserRelUser::USER_RELATION_TYPE_FRIEND)
;
$this->entityManager->persist($connection);
$this->entityManager->flush();
}
}
}
return $result;
}
}
Loading…
Cancel
Save