Internal: API: Replace UserItemDataProvider with UserProvider

pull/5329/head
Angel Fernando Quiroz Campos 7 months ago
parent 4b7e83b3e9
commit 438d39efe0
  1. 35
      src/CoreBundle/DataProvider/UserItemDataProvider.php
  2. 2
      src/CoreBundle/Entity/User.php
  3. 49
      src/CoreBundle/State/UserProvider.php

@ -1,35 +0,0 @@
<?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\DataProvider;
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use Chamilo\CoreBundle\Component\Utils\NameConvention;
use Chamilo\CoreBundle\Entity\User;
use Chamilo\CoreBundle\Repository\Node\UserRepository;
class UserItemDataProvider implements ItemDataProviderInterface, RestrictedDataProviderInterface
{
public function __construct(
private readonly UserRepository $repository,
private readonly NameConvention $nameConvention
) {}
public function getItem(string $resourceClass, $id, ?string $operationName = null, array $context = []): ?object
{
$user = $this->repository->find($id);
$this->nameConvention->getPersonName($user);
return $user;
}
public function supports(string $resourceClass, ?string $operationName = null, array $context = []): bool
{
return User::class === $resourceClass;
}
}

@ -19,6 +19,7 @@ use ApiPlatform\Metadata\Put;
use Chamilo\CoreBundle\Controller\Api\UserSkillsController;
use Chamilo\CoreBundle\Entity\Listener\UserListener;
use Chamilo\CoreBundle\Repository\Node\UserRepository;
use Chamilo\CoreBundle\State\UserProvider;
use Chamilo\CoreBundle\Traits\UserCreatorTrait;
use Chamilo\CourseBundle\Entity\CGroupRelTutor;
use Chamilo\CourseBundle\Entity\CGroupRelUser;
@ -62,6 +63,7 @@ use UserManager;
],
normalizationContext: ['groups' => ['user:read']],
denormalizationContext: ['groups' => ['user:write']],
provider: UserProvider::class,
security: 'is_granted("ROLE_USER")'
)]
#[ORM\Table(name: 'user')]

@ -0,0 +1,49 @@
<?php
/* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\State;
use ApiPlatform\Doctrine\Orm\Paginator;
use ApiPlatform\Doctrine\Orm\State\CollectionProvider;
use ApiPlatform\Doctrine\Orm\State\ItemProvider;
use ApiPlatform\Metadata\CollectionOperationInterface;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use Chamilo\CoreBundle\Component\Utils\NameConvention;
use Chamilo\CoreBundle\Entity\User;
/**
* @template-implements ProviderInterface<User>
*/
class UserProvider implements ProviderInterface
{
public function __construct(
private readonly ItemProvider $itemProvider,
private readonly CollectionProvider $collectionProvider,
private readonly NameConvention $nameConvention
) {}
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
if ($operation instanceof CollectionOperationInterface) {
$providedUsers = $this->collectionProvider->provide($operation, $uriVariables, $context);
assert($providedUsers instanceof Paginator);
/** @var User $user */
foreach ($providedUsers as $user) {
$this->nameConvention->getPersonName($user);
}
return $providedUsers;
}
/** @var User $user */
$user = $this->itemProvider->provide($operation, $uriVariables, $context);
$this->nameConvention->getPersonName($user);
return $user;
}
}
Loading…
Cancel
Save