<?php
declare(strict_types=1);
namespace App\V4\Controller\Task;
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
use App\V4\Controller\AbstractGetSubListAction;
use App\V4\Model\Task\Task;
use App\V4\Model\TaskType\TaskType;
use App\V4\Voters\TaskVoter;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;
class GetTasksByProspectAction extends AbstractGetSubListAction
{
const ENTITY = Task::class;
protected const ACCESS_UNLESS_GRANTED = TaskVoter::TASK_SHOW_LIST;
/**
* @param CollectionDataProviderInterface $collectionDataProvider
* @param Serializer $serializer
* @param ItemDataProviderInterface $itemDataProvider
*/
public function __construct(
CollectionDataProviderInterface $collectionDataProvider,
SerializerInterface $serializer,
ItemDataProviderInterface $itemDataProvider
) {
parent::__construct($collectionDataProvider, $serializer, $itemDataProvider);
}
/**
* @param Request $request
*
* @return array
*
* @throws ResourceClassNotSupportedException
*/
protected function getDefaultFilters(Request $request): array
{
$taskTypeIds = null;
if ('true' !== $request->get('isMailingTask') && 'true' !== $request->get('isSynchroOutlookTask')) {
$taskTypes = $this->collectionDataProvider
->getCollection(TaskType::class, null, [
'filters' => [
'isMailing' => '0',
],
AbstractGetSubListAction::CONTEXT_IS_SUB_RESOURCE_KEY => true,
])
;
$taskTypeIds = ['empty'];
foreach ($taskTypes as $taskType) {
$taskTypeIds[] = $taskType->getId();
}
}
return [
self::KEY_PROSPECT => $request->get('id'),
'isClosed' => $request->get('isClosed'),
'taskType.isSynchroOutlook' => $request->get('isSynchroOutlookTask') ?? 'false',
'taskType.isMailing' => $request->get('isMailingTask'),
'taskType' => $taskTypeIds,
];
}
}