Skip to content
Snippets Groups Projects
Select Git revision
  • f27593b98d5d5a5169b168443e07d0a80f4f88bc
  • main default protected
  • renovate/lock-file-maintenance
  • demo protected
  • person-select-custom
  • dbp-translation-component
  • icon-set-mapping
  • port-i18next-parser
  • remove-sentry
  • favorites-and-recent-files
  • revert-6c632dc6
  • lit2
  • advertisement
  • wc-part
  • automagic
  • publish
  • wip-cleanup
  • demo-file-handling
18 results

README.md

Blame
  • CourseCollectionDataProvider.php 1.61 KiB
    <?php
    
    declare(strict_types=1);
    
    namespace Dbp\Relay\BaseCourseBundle\DataProvider;
    
    use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
    use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
    use Dbp\Relay\BaseCourseBundle\API\CourseProviderInterface;
    use Dbp\Relay\BaseCourseBundle\Entity\Course;
    use Dbp\Relay\CoreBundle\Helpers\ArrayFullPaginator;
    use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
    
    final class CourseCollectionDataProvider extends AbstractController implements CollectionDataProviderInterface, RestrictedDataProviderInterface
    {
        private $api;
    
        public function __construct(CourseProviderInterface $api)
        {
            $this->api = $api;
        }
    
        public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
        {
            return Course::class === $resourceClass;
        }
    
        public function getCollection(string $resourceClass, string $operationName = null, array $context = []): ArrayFullPaginator
        {
            $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
    
            $filters = $context['filters'] ?? [];
            $options = ['lang' => $filters['lang'] ?? 'de'];
    
            $term = $filters['term'] ?? null;
            if ($term !== null) {
                $options['term'] = $term;
            }
    
            $page = 1;
            if (isset($filters['page'])) {
                $page = (int) $filters['page'];
            }
    
            $perPage = 30;
            if (isset($filters['perPage'])) {
                $perPage = (int) $filters['perPage'];
            }
    
            return new ArrayFullPaginator($this->api->getCourses($options), $page, $perPage);
        }
    }