77namespace Chamilo \CoreBundle \State ;
88
99use ApiPlatform \Doctrine \Orm \Paginator ;
10- use ApiPlatform \Doctrine \Orm \State \CollectionProvider ;
11- use ApiPlatform \Doctrine \Orm \State \ItemProvider ;
1210use ApiPlatform \Metadata \CollectionOperationInterface ;
1311use ApiPlatform \Metadata \Operation ;
1412use ApiPlatform \State \ProviderInterface ;
1513use Chamilo \CoreBundle \Entity \Message ;
16- use Doctrine \ Common \ Collections \ Collection ;
14+ use Chamilo \ CoreBundle \ Helpers \ UserHelper ;
1715use Doctrine \ORM \EntityManagerInterface ;
1816use Doctrine \ORM \QueryBuilder ;
19- use LogicException ;
20- use Symfony \Bundle \SecurityBundle \Security ;
17+ use Symfony \Component \DependencyInjection \Attribute \Autowire ;
2118use Symfony \Component \HttpFoundation \RequestStack ;
19+ use Symfony \Component \HttpKernel \Exception \AccessDeniedHttpException ;
2220
2321/**
2422 * @template-implements ProviderInterface<Message>
2523 */
2624final class MessageStateProvider implements ProviderInterface
2725{
2826 public function __construct (
29- private readonly CollectionProvider $ collectionProvider ,
30- private readonly ItemProvider $ itemProvider ,
27+ #[Autowire(service: 'api_platform.doctrine.orm.state.collection_provider ' )]
28+ private readonly ProviderInterface $ collectionProvider ,
29+ #[Autowire(service: 'api_platform.doctrine.orm.state.item_provider ' )]
30+ private readonly ProviderInterface $ itemProvider ,
3131 private readonly EntityManagerInterface $ entityManager ,
32- private readonly Security $ security ,
32+ private readonly UserHelper $ userHelper ,
3333 private readonly RequestStack $ requestStack
3434 ) {}
3535
3636 /**
3737 * Provides data based on the operation type (collection or item).
3838 */
39- public function provide (Operation $ operation , array $ uriVariables = [], array $ context = []): Paginator | Message |null
39+ public function provide (Operation $ operation , array $ uriVariables = [], array $ context = []): array | object |null
4040 {
4141 if ($ operation instanceof CollectionOperationInterface) {
4242 return $ this ->handleCollection ($ operation , $ context );
@@ -49,11 +49,11 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
4949 /**
5050 * Handles collection-level operations with filtering and pagination.
5151 */
52- private function handleCollection (Operation $ operation , array $ context ): Paginator
52+ private function handleCollection (Operation $ operation , array $ context ): array | Paginator
5353 {
54- $ user = $ this ->security -> getUser ();
54+ $ user = $ this ->userHelper -> getCurrent ();
5555 if (!$ user ) {
56- throw new LogicException ('User not found. ' );
56+ throw new AccessDeniedHttpException ('User not found. ' );
5757 }
5858
5959 // Retrieve initial filters if they exist
@@ -75,10 +75,23 @@ private function handleCollection(Operation $operation, array $context): Paginat
7575 return $ this ->collectionProvider ->provide ($ operation , [], $ context );
7676 }
7777
78- // Build custom query for advanced filters
78+ // Build a custom query for advanced filters
7979 return $ this ->applyCustomQuery ($ operation , $ context , $ filters );
8080 }
8181
82+ /**
83+ * Merges request filters into the provided filter array.
84+ */
85+ private function applyFilters (array &$ filters ): void
86+ {
87+ $ request = $ this ->requestStack ->getMainRequest ();
88+
89+ if ($ request ) {
90+ $ requestFilters = $ request ->query ->all ();
91+ $ filters = array_merge ($ filters , $ requestFilters );
92+ }
93+ }
94+
8295 /**
8396 * Builds and applies a custom query with filtering, sorting, and pagination.
8497 */
@@ -99,8 +112,8 @@ private function applyCustomQuery(Operation $operation, array $context, array $f
99112 ;
100113
101114 // Count query for total items
102- $ countQueryBuilder = $ this ->createQueryWithFilters ($ filters , true );
103- $ totalItems = (int ) $ countQueryBuilder ->getQuery ()->getSingleScalarResult ();
115+ // $countQueryBuilder = $this->createQueryWithFilters($filters, true);
116+ // $totalItems = (int) $countQueryBuilder->getQuery()->getSingleScalarResult();
104117
105118 // Doctrine Paginator
106119 $ doctrinePaginator = new \Doctrine \ORM \Tools \Pagination \Paginator ($ queryBuilder , true );
@@ -171,16 +184,4 @@ private function createQueryWithFilters(array $filters, bool $isCountQuery = fal
171184
172185 return $ queryBuilder ;
173186 }
174-
175- /**
176- * Merges request filters into the provided filter array.
177- */
178- private function applyFilters (array &$ filters ): void
179- {
180- $ request = $ this ->requestStack ->getMainRequest ();
181- if ($ request ) {
182- $ requestFilters = $ request ->query ->all ();
183- $ filters = array_merge ($ filters , $ requestFilters );
184- }
185- }
186187}
0 commit comments