Skip to content
Snippets Groups Projects
CourseCollectionDataProvider.php 2.9 KiB
Newer Older
Tobias Gross-Vogt's avatar
Tobias Gross-Vogt committed
<?php

declare(strict_types=1);

namespace Dbp\Relay\BaseCourseBundle\DataProvider;
Tobias Gross-Vogt's avatar
Tobias Gross-Vogt committed

use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use Dbp\Relay\BaseCourseBundle\API\CourseProviderInterface;
use Dbp\Relay\BaseCourseBundle\Entity\Course;
Tobias Gross-Vogt's avatar
Tobias Gross-Vogt committed
use Dbp\Relay\CoreBundle\Helpers\ArrayFullPaginator;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

final class CourseCollectionDataProvider extends AbstractController implements CollectionDataProviderInterface, RestrictedDataProviderInterface
{
    private $courseProvider;
Tobias Gross-Vogt's avatar
Tobias Gross-Vogt committed

    public function __construct(CourseProviderInterface $courseProvider)
Tobias Gross-Vogt's avatar
Tobias Gross-Vogt committed
    {
        $this->courseProvider = $courseProvider;
Tobias Gross-Vogt's avatar
Tobias Gross-Vogt committed
    }

    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
    {
Tobias Gross-Vogt's avatar
Tobias Gross-Vogt committed
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
Tobias Gross-Vogt's avatar
Tobias Gross-Vogt committed

Tobias Gross-Vogt's avatar
Tobias Gross-Vogt committed
        $filters = $context['filters'] ?? [];
        $options['lang'] = $filters['lang'] ?? 'de';
        $term = $filters['term'] ?? null;
        if ($term !== null) {
            $options['term'] = $term;
        }

        $organizationId = $filters['organizationId'] ?? null;
        $personId = $filters['personId'] ?? null;

        $courses = null;
        if (!empty($organizationId) || !empty($personId)) {
            if (!empty($organizationId)) {
                $courses = $this->courseProvider->getCoursesByOrganization($organizationId, $options);
            }
            if (!empty($personId)) {
                $coursesByPerson = $this->courseProvider->getCoursesByPerson($personId, $options);
                if (!empty($organizationId)) {
                    $courses = array_uintersect($courses, $coursesByPerson,
                        'Dbp\Relay\BaseCourseBundle\DataProvider\CourseCollectionDataProvider::compareCourses');
                    $courses = array_values($courses);
                } else {
                    $courses = $coursesByPerson;
                }
            }
        } else {
            $courses = $this->courseProvider->getCourses($options);
        }

Tobias Gross-Vogt's avatar
Tobias Gross-Vogt committed
        $page = 1;
Tobias Gross-Vogt's avatar
Tobias Gross-Vogt committed
        if (isset($filters['page'])) {
            $page = (int) $filters['page'];
        }
Tobias Gross-Vogt's avatar
Tobias Gross-Vogt committed

        $perPage = 30;
Tobias Gross-Vogt's avatar
Tobias Gross-Vogt committed
        if (isset($filters['perPage'])) {
            $perPage = (int) $filters['perPage'];
        }

        return new ArrayFullPaginator($courses, $page, $perPage);
    }

    public static function compareCourses(Course $a, Course $b): int
    {
        if ($a->getIdentifier() > $b->getIdentifier()) {
            return 1;
        } elseif ($a->getIdentifier() === $b->getIdentifier()) {
Tobias Gross-Vogt's avatar
Tobias Gross-Vogt committed
    }
}