Skip to content

Instantly share code, notes, and snippets.

@bernard-ng
Last active September 15, 2021 23:32
Show Gist options
  • Select an option

  • Save bernard-ng/b1b61d56a5ffc875c5f20529f0d266d1 to your computer and use it in GitHub Desktop.

Select an option

Save bernard-ng/b1b61d56a5ffc875c5f20529f0d266d1 to your computer and use it in GitHub Desktop.

Revisions

  1. bernard-ng revised this gist Sep 15, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions DisallowedCountryRepository.php
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@ class DisallowCountryRepository extends ServiceEntityRepository
    * DisallowCountryRepository constructor.
    * @param ManagerRegistry $registry
    * @param LoggerInterface $logger
    * @author bernard-ng <[email protected]>
    * @author bernard-ng <[email protected]>
    */
    public function __construct(ManagerRegistry $registry, LoggerInterface $logger)
    {
    @@ -34,7 +34,7 @@ public function __construct(ManagerRegistry $registry, LoggerInterface $logger)

    /**
    * @return array
    * @author bernard-ng <[email protected]>
    * @author bernard-ng <[email protected]>
    */
    public function getDisallowedCountries(): array
    {
  2. bernard-ng created this gist Mar 1, 2021.
    60 changes: 60 additions & 0 deletions DisallowedCountryRepository.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    <?php

    declare(strict_types=1);

    namespace App\Repository;

    use App\Entity\DisallowCountry;
    use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
    use Doctrine\Persistence\ManagerRegistry;
    use Exception;
    use Psr\Log\LoggerInterface;

    /**
    * @method DisallowCountry|null find($id, $lockMode = null, $lockVersion = null)
    * @method DisallowCountry|null findOneBy(array $criteria, array $orderBy = null)
    * @method DisallowCountry[] findAll()
    * @method DisallowCountry[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
    */
    class DisallowCountryRepository extends ServiceEntityRepository
    {
    private LoggerInterface $logger;

    /**
    * DisallowCountryRepository constructor.
    * @param ManagerRegistry $registry
    * @param LoggerInterface $logger
    * @author bernard-ng <[email protected]>
    */
    public function __construct(ManagerRegistry $registry, LoggerInterface $logger)
    {
    parent::__construct($registry, DisallowCountry::class);
    $this->logger = $logger;
    }

    /**
    * @return array
    * @author bernard-ng <[email protected]>
    */
    public function getDisallowedCountries(): array
    {
    $sql = <<< SQL
    SELECT iso2 FROM disallow_country
    WHERE has_access = '0'
    ORDER BY name
    SQL;

    try {
    $connexion = $this->_em->getConnection();
    $statement = $connexion->prepare($sql);
    $statement->execute();

    // important nous avons d'un tableau contenant unique la list de pays
    // ['CD', 'CA', 'FR', 'US'] par exemple
    return $statement->fetchFirstColumn();
    } catch (Exception | \Doctrine\DBAL\Driver\Exception $e) {
    $this->logger->error($e->getMessage(), $e->getTrace());
    return [];
    }
    }
    }