Getting rid of magic number

This commit is contained in:
Bob den Otter
2019-03-26 15:40:49 +01:00
parent 1302f9b708
commit ed445a0f64
3 changed files with 16 additions and 13 deletions

View File

@@ -39,9 +39,10 @@ class TaxonomyController extends TwigAwareController
public function listing(ContentRepository $contentRepository, Request $request, string $taxonomyslug, string $slug): Response
{
$page = (int) $request->query->get('page', 1);
$amountPerPage = $this->config->get('general/listing_records');
/** @var Content[] $records */
$records = $contentRepository->findForTaxonomy($page, $taxonomyslug, $slug, $this->config->get('listing_records'));
$records = $contentRepository->findForTaxonomy($page, $taxonomyslug, $slug, $amountPerPage);
$templates = $this->templateChooser->forTaxonomy($taxonomyslug);

View File

@@ -11,23 +11,23 @@ abstract class BaseFixture extends Fixture
private $referencesIndex = [];
private $taxonomyIndex = [];
protected function getRandomReference(string $className)
protected function getRandomReference(string $entityName)
{
if (! isset($this->referencesIndex[$className])) {
$this->referencesIndex[$className] = [];
if (! isset($this->referencesIndex[$entityName])) {
$this->referencesIndex[$entityName] = [];
foreach (array_keys($this->referenceRepository->getReferences()) as $key) {
if (mb_strpos($key, $className.'_') === 0) {
$this->referencesIndex[$className][] = $key;
if (mb_strpos($key, $entityName.'_') === 0) {
$this->referencesIndex[$entityName][] = $key;
}
}
}
if (empty($this->referencesIndex[$className])) {
throw new \Exception(sprintf('Cannot find any references for class "%s"', $className));
if (empty($this->referencesIndex[$entityName])) {
throw new \Exception(sprintf('Cannot find any references for Entity "%s"', $entityName));
}
$randomReferenceKey = array_rand($this->referencesIndex[$className], 1);
$randomReferenceKey = array_rand($this->referencesIndex[$entityName], 1);
return $this->getReference($this->referencesIndex[$className][$randomReferenceKey]);
return $this->getReference($this->referencesIndex[$entityName][$randomReferenceKey]);
}
protected function getRandomTaxonomy(string $type)

View File

@@ -48,10 +48,12 @@ class ContentRepository extends ServiceEntityRepository
->setParameter('status', Statuses::PUBLISHED);
}
return $this->createPaginator($qb->getQuery(), $page, $contentType['listing_records']);
$amountPerPage = $contentType['listing_records'];
return $this->createPaginator($qb->getQuery(), $page, $amountPerPage);
}
public function findForTaxonomy(int $page, string $taxonomyslug, string $slug, ?int $amountPerPage = null, bool $onlyPublished = true): Pagerfanta
public function findForTaxonomy(int $page, string $taxonomyslug, string $slug, int $amountPerPage, bool $onlyPublished = true): Pagerfanta
{
$qb = $this->getQueryBuilder()
->addSelect('a')
@@ -105,7 +107,7 @@ class ContentRepository extends ServiceEntityRepository
private function createPaginator(Query $query, int $page, ?int $amountPerPage): Pagerfanta
{
$paginator = new Pagerfanta(new DoctrineORMAdapter($query));
$paginator->setMaxPerPage($amountPerPage ?: 6);
$paginator->setMaxPerPage($amountPerPage);
$paginator->setCurrentPage($page);
return $paginator;