Skip to content

Instantly share code, notes, and snippets.

@bernard-ng
Last active September 15, 2021 23:32
Show Gist options
  • Save bernard-ng/b1b61d56a5ffc875c5f20529f0d266d1 to your computer and use it in GitHub Desktop.
Save bernard-ng/b1b61d56a5ffc875c5f20529f0d266d1 to your computer and use it in GitHub Desktop.
<?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 <bernard@devscast.tech>
*/
public function __construct(ManagerRegistry $registry, LoggerInterface $logger)
{
parent::__construct($registry, DisallowCountry::class);
$this->logger = $logger;
}
/**
* @return array
* @author bernard-ng <bernard@devscast.tech>
*/
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 [];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment