Commit 3b8a3618 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Refonte pour permettre la création manuelle d'alertes (incompatible avec version 2)

parent d9f076ab
Loading
Loading
Loading
Loading
+39 −0
Original line number Diff line number Diff line
<?php

namespace UnicaenAlerte\Container\Array;

use UnicaenAlerte\Entity\Db\Alerte;

class ArrayContainer implements ArrayContainerInterface
{
    /**
     * Alertes à afficher.
     * @var Alerte[]
     */
    protected array $alertes = [];

    public function addAlerte(Alerte $alerte): self
    {
        $this->alertes[] = $alerte;

        return $this;
    }

    public function addAlerteNew(string $text,
                                 string $title,
                                 string $severity = Alerte::SEVERITY_INFO,
                                 int    $duration = 0): self
    {
        $alerte = Alerte::create(uniqid('ALERTE_'), $text, $title, $severity, $duration);

        return $this->addAlerte($alerte);
    }

    /**
     * @return array|\UnicaenAlerte\Entity\Db\Alerte[]
     */
    public function fetchAlertes(): array
    {
        return $this->alertes;
    }
}
 No newline at end of file
+16 −0
Original line number Diff line number Diff line
<?php

namespace UnicaenAlerte\Container\Array;

use UnicaenAlerte\Container\ContainerInterface;
use UnicaenAlerte\Entity\Db\Alerte;

interface ArrayContainerInterface extends ContainerInterface
{
    public function addAlerte(Alerte $alerte): self;

    public function addAlerteNew(string $text,
                                 string $title,
                                 string $severity = Alerte::SEVERITY_INFO,
                                 int    $duration = 0): self;
}
 No newline at end of file
+13 −0
Original line number Diff line number Diff line
<?php

namespace UnicaenAlerte\Container;

interface ContainerInterface
{
    /**
     * Fetch des alertes à afficher.
     *
     * @return \UnicaenAlerte\Entity\Db\Alerte[]
     */
    public function fetchAlertes(): array;
}
 No newline at end of file
+58 −0
Original line number Diff line number Diff line
<?php

namespace UnicaenAlerte\Container\Database;

use Doctrine\ORM\Query\Expr\Join;
use UnicaenAlerte\Container\FilterableContainerInterface;
use UnicaenAlerte\Container\FilterableContainerTrait;
use UnicaenAlerte\Entity\Db\Alerte;
use UnicaenAlerte\Entity\Db\Repository\AlerteRepositoryAwareTrait;

class DatabaseContainer implements FilterableContainerInterface
{
    use FilterableContainerTrait;
    use AlerteRepositoryAwareTrait;

    public function fetchAlertes(): array
    {
        $qb = $this->alerteRepository->createQueryBuilder('a')
            ->addSelect('p') // seuls les plannings (éventuels) incluant la date du jour nous intéressent
            ->leftJoin('a.plannings', 'p', Join::WITH, 'current_timestamp() between p.startDate and p.endDate');

        $ors = [];
        foreach ($this->withCodesAndMatchingPlanning as $code => $matchingPlanning) {
            // le code de l'alerte doit matcher
            $param = uniqid(':code_');
            $expr = "a.code = $param";
            $qb->setParameter($param, $code);
            if ($matchingPlanning) {
                // et en plus, un planning doit exister
                $expr = $qb->expr()->andX($expr, 'p.id is not null');
            }
            $ors[] = $expr;
        }
        if ($ors) {
            $qb->andWhere($qb->expr()->orX(...$ors));
        }

        // filtre global portant sur toutes les alertes.
        if ($this->withMatchingPlanning) {
            $qb->andWhere('p.id is not null');
        }

        /** @var Alerte[] $alertes */
        $alertes = $qb->getQuery()->getResult();

        // la sévérité spécifiée dans l'eventuel planning écrase la sévérité par défaut :
        foreach ($alertes as $alerte) {
            /** @var \UnicaenAlerte\Entity\Db\AlertePlanning $planning */
            $planning = $alerte->getPlannings()->first(); // un seul planning en cours pris en compte
            if ($planning && $severity = $planning->getSeverity()) {
                $alerte->setSeverity($severity);
                break;
            }
        }

        return $alertes;
    }
}
 No newline at end of file
+45 −0
Original line number Diff line number Diff line
<?php

namespace UnicaenAlerte\Container;

interface FilterableContainerInterface extends ContainerInterface
{
    /**
     * Sélection précise des alertes par leur code.
     *
     * @param string[] $codes
     */
    public function withCodes(array $codes): static;

    /**
     * Sélection précise des alertes par leur code,
     * en précisant si les alertes doivent avoir un planning qui inclue la date du jour.
     *
     * @param string[] $codes
     * @param bool $matchingPlanning L'alerte doit-elle avoir un planning qui inclue la date du jour pour être retenue ?
     */
    public function withCodesAndMatchingPlanning(array $codes, bool $matchingPlanning = true): static;

    /**
     * Sélection précise d'une alerte par son code.
     *
     * @param string $code
     */
    public function withCode(string $code): static;

    /**
     * Sélection précise d'une alerte par son code,
     * en précisant si l'alerte doit avoir un planning qui inclue la date du jour.
     *
     * @param string $code
     * @param bool $matchingPlanning L'alerte doit-elle avoir un planning qui inclue la date du jour pour être retenue ?
     */
    public function withCodeAndMatchingPlanning(string $code, bool $matchingPlanning = true): static;

    /**
     * Permet de ne sélectionner que les alertes ayant un planning qui inclue la date du jour.
     *
     * @param bool $matchingPlanning L'alerte doit-elle avoir un planning qui inclue la date du jour pour être retenue ?
     */
    public function withMatchingPlanning(bool $matchingPlanning = true): static;
}
 No newline at end of file
Loading