Skip to content

Commit

Permalink
Resolve PhpStan errors and use Repository.
Browse files Browse the repository at this point in the history
  • Loading branch information
janbarasek committed Jan 9, 2022
1 parent 9f1b13f commit 0109eec
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 49 deletions.
6 changes: 2 additions & 4 deletions src/Cms/CmsCountryEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@


use Baraja\Country\CountryManagerAccessor;
use Baraja\Doctrine\EntityManager;
use Baraja\StructuredApi\BaseEndpoint;

final class CmsCountryEndpoint extends BaseEndpoint
{
public function __construct(
private EntityManager $entityManager,
private CountryManagerAccessor $countryManagerAccessor,
) {
}
Expand All @@ -38,7 +36,7 @@ public function actionDefault(): void
$this->sendJson(
[
'countries' => $countries,
]
],
);
}

Expand All @@ -48,7 +46,7 @@ public function postSetActive(int $id): void
$country = $this->countryManagerAccessor->get()->getById($id);
$this->countryManagerAccessor->get()->setActive($country, !$country->isActive());
$this->flashMessage(
'Country has been marked as ' . ($country->isActive() ? 'active' : 'hidden') . '.',
sprintf('Country has been marked as %s.', $country->isActive() ? 'active' : 'hidden'),
self::FLASH_MESSAGE_SUCCESS,
);
$this->sendOk();
Expand Down
38 changes: 13 additions & 25 deletions src/CountryManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,31 @@


use Baraja\Country\Entity\Country;
use Baraja\Doctrine\EntityManager;
use Baraja\Country\Entity\CountryRepository;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\NoResultException;

final class CountryManager
{
private CountryRepository $countryRepository;


public function __construct(
private EntityManager $entityManager,
private EntityManagerInterface $entityManager,
) {
$countryRepository = $entityManager->getRepository(Country::class);
assert($countryRepository instanceof CountryRepository);
$this->countryRepository = $countryRepository;
}


/**
* @return Country[]
* @return array<int, Country>
*/
public function getAll(): array
{
/** @var Country[] $list */
$list = $this->entityManager->getRepository(Country::class)
->createQueryBuilder('country')
->orderBy('country.active', 'DESC')
->addOrderBy('country.name', 'ASC')
->getQuery()
->getResult();

$list = $this->countryRepository->getAll();
if ($list === []) {
$this->sync();
$list = $this->getAll();
Expand All @@ -45,26 +45,14 @@ public function getAll(): array
*/
public function getById(int $id): Country
{
return $this->entityManager->getRepository(Country::class)
->createQueryBuilder('country')
->where('country.id = :id')
->setParameter('id', $id)
->setMaxResults(1)
->getQuery()
->getSingleResult();
return $this->countryRepository->getById($id);
}


public function getByCode(string $code): Country
{
try {
return $this->entityManager->getRepository(Country::class)
->createQueryBuilder('country')
->where('country.code = :code OR country.isoCode = :code')
->setParameter('code', $code)
->setMaxResults(1)
->getQuery()
->getSingleResult();
return $this->countryRepository->getByCode($code);
} catch (NoResultException | NonUniqueResultException $e) {
if ($this->sync() === true) {
return $this->getByCode($code);
Expand Down
34 changes: 16 additions & 18 deletions src/CountrySynchronizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@


use Baraja\Country\Entity\Country;
use Baraja\Doctrine\EntityManager;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\NoResultException;
use Baraja\Country\Entity\CountryRepository;
use Doctrine\ORM\EntityManagerInterface;

final class CountrySynchronizer
{
Expand All @@ -28,25 +27,21 @@ final class CountrySynchronizer

private string $currency = 'http://country.io/currency.json';

private CountryRepository $countryRepository;


public function __construct(
private EntityManager $entityManager,
private EntityManagerInterface $entityManager,
) {
$countryRepository = $entityManager->getRepository(Country::class);
assert($countryRepository instanceof CountryRepository);
$this->countryRepository = $countryRepository;
}


public function run(): bool
{
try {
$countries = (int) $this->entityManager->getRepository(Country::class)
->createQueryBuilder('country')
->select('COUNT(country.id)')
->getQuery()
->getSingleScalarResult();
} catch (NoResultException | NonUniqueResultException) {
$countries = 0;
}
if ($countries > 0) {
if ($this->countryRepository->getCount() > 0) {
return false;
}

Expand Down Expand Up @@ -99,13 +94,16 @@ public function run(): bool
private function download(string $url): array
{
$data = (string) file_get_contents($url);
if (!$data) {
throw new \InvalidArgumentException('API response for URL "' . $url . '" is empty.');
if ($data === '') {
throw new \InvalidArgumentException(sprintf('API response for URL "%s" is empty.', $url));
}
try {
return (array) json_decode($data, true, 512, JSON_THROW_ON_ERROR);
/** @var array<string, string> $return */
$return = (array) json_decode($data, true, 512, JSON_THROW_ON_ERROR);

return $return;
} catch (\Throwable $e) {
throw new \InvalidArgumentException('Invalid json response: ' . $e->getMessage(), $e->getCode(), $e);
throw new \InvalidArgumentException(sprintf('Invalid json response: %s', $e->getMessage()), 500, $e);
}
}
}
4 changes: 2 additions & 2 deletions src/CountryToEmoji.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static function getByCode(string $code): string
return self::$map[$code];
}

throw new \InvalidArgumentException('Country flag for code "' . $code . '" does not exist.');
throw new \InvalidArgumentException(sprintf('Country flag for code "%s" does not exist.', $code));
}


Expand All @@ -39,7 +39,7 @@ private static function load(): void
}
$source = __DIR__ . '/../flag-emoji.json';
if (!is_file($source)) {
throw new \LogicException('Emoji map file does not exist. Path "' . $source . '" given.');
throw new \LogicException(sprintf('Emoji map file does not exist. Path "%s" given.', $source));
}
/** @var array<string, string> $map */
$map = json_decode((string) file_get_contents($source), true, 512, JSON_THROW_ON_ERROR);
Expand Down
66 changes: 66 additions & 0 deletions src/Entity/CountryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,73 @@


use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\NoResultException;

final class CountryRepository extends EntityRepository
{
/**
* @throws NoResultException|NonUniqueResultException
*/
public function getById(int $id): Country
{
$return = $this->createQueryBuilder('country')
->where('country.id = :id')
->setParameter('id', $id)
->setMaxResults(1)
->getQuery()
->getSingleResult();
assert($return instanceof Country);

return $return;
}


/**
* @throws NoResultException|NonUniqueResultException
*/
public function getByCode(string $code): Country
{
$return = $this->createQueryBuilder('country')
->where('country.code = :code OR country.isoCode = :code')
->setParameter('code', $code)
->setMaxResults(1)
->getQuery()
->getSingleResult();
assert($return instanceof Country);

return $return;
}


public function getCount(): int
{
try {
/** @var int $count */
$count = $this->createQueryBuilder('country')
->select('COUNT(country.id)')
->getQuery()
->getSingleScalarResult();
} catch (NoResultException | NonUniqueResultException) {
$count = 0;
}

return $count;
}


/**
* @return array<int, Country>
*/
public function getAll(): array
{
/** @var array<int, Country> $list */
$list = $this->createQueryBuilder('country')
->orderBy('country.active', 'DESC')
->addOrderBy('country.name', 'ASC')
->getQuery()
->getResult();

return $list;
}
}

0 comments on commit 0109eec

Please sign in to comment.