Covivo/mobicoop

View on GitHub
api/src/User/Filter/CardLetterFilter.php

Summary

Maintainability
C
1 day
Test Coverage
<?php
/**
 * Copyright (c) 2022, MOBICOOP. All rights reserved.
 * This project is dual licensed under AGPL and proprietary licence.
 ***************************
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU Affero General Public License as
 *    published by the Free Software Foundation, either version 3 of the
 *    License, or (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <gnu.org/licenses>.
 ***************************
 *    Licence MOBICOOP described in the file
 *    LICENSE
 */

namespace App\User\Filter;

use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\AbstractContextAwareFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use App\User\Entity\User;
use Doctrine\ORM\QueryBuilder;

final class CardLetterFilter extends AbstractContextAwareFilter
{
    // This function is only used to hook in documentation generators (supported by Swagger and Hydra)
    public function getDescription(string $resourceClass): array
    {
        if (!$this->properties) {
            return [];
        }

        $description = [];
        foreach ($this->properties as $property => $strategy) {
            $description[$property] = [
                'property' => $property,
                'type' => 'boolean',
                'required' => false,
                'swagger' => [
                    'description' => 'Filter on users with sending status of cardLetter',
                    'name' => 'cardLetter',
                    'type' => 'boolean',
                ],
            ];
        }

        return $description;
    }

    protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null)
    {
        if ('cardLetter' != $property) {
            return;
        }

        $queryBuilder
            ->andWhere('u.status != :status')
            ->setParameters(['status' => User::STATUS_PSEUDONYMIZED])
        ;

        if (true === json_decode($value)) {
            $queryBuilder
                ->andWhere('u.cardLetter = \''.json_decode($value).'\'')
            ;
        } elseif (false === json_decode($value)) {
            $queryBuilder
                ->andWhere('u.cardLetter = \''.json_decode($value).'\'')
                ->orWhere('u.cardLetter IS null')
            ;
        } else {
            return;
        }
    }
}