Skip to content

Commit

Permalink
Move some constant to a better place
Browse files Browse the repository at this point in the history
  • Loading branch information
javiereguiluz committed Oct 8, 2020
1 parent f8cfb9c commit 0de89e1
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
8 changes: 0 additions & 8 deletions src/Entity/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,6 @@
*/
class Post
{
/**
* Use constants to define configuration options that rarely change instead
* of specifying them under parameters section in config/services.yaml file.
*
* See https://symfony.com/doc/current/best_practices.html#use-constants-to-define-options-that-rarely-change
*/
public const NUM_ITEMS = 10;

/**
* @var int
*
Expand Down
11 changes: 9 additions & 2 deletions src/Pagination/Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace App\Pagination;

use App\Entity\Post;
use Doctrine\ORM\QueryBuilder as DoctrineQueryBuilder;
use Doctrine\ORM\Tools\Pagination\CountWalker;
use Doctrine\ORM\Tools\Pagination\Paginator as DoctrinePaginator;
Expand All @@ -21,13 +20,21 @@
*/
class Paginator
{
/**
* Use constants to define configuration options that rarely change instead
* of specifying them under parameters section in config/services.yaml file.
*
* See https://symfony.com/doc/current/best_practices.html#use-constants-to-define-options-that-rarely-change
*/
public const PAGE_SIZE = 10;

private $queryBuilder;
private $currentPage;
private $pageSize;
private $results;
private $numResults;

public function __construct(DoctrineQueryBuilder $queryBuilder, int $pageSize = Post::NUM_ITEMS)
public function __construct(DoctrineQueryBuilder $queryBuilder, int $pageSize = self::PAGE_SIZE)
{
$this->queryBuilder = $queryBuilder;
$this->pageSize = $pageSize;
Expand Down
2 changes: 1 addition & 1 deletion src/Repository/PostRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function findLatest(int $page = 1, Tag $tag = null): Paginator
/**
* @return Post[]
*/
public function findBySearchQuery(string $query, int $limit = Post::NUM_ITEMS): array
public function findBySearchQuery(string $query, int $limit = Paginator::PAGE_SIZE): array
{
$searchTerms = $this->extractSearchTerms($query);

Expand Down
5 changes: 3 additions & 2 deletions tests/Controller/BlogControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace App\Tests\Controller;

use App\Entity\Post;
use App\Pagination\Paginator;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

/**
Expand All @@ -34,7 +35,7 @@ public function testIndex(): void
$this->assertResponseIsSuccessful();

$this->assertCount(
Post::NUM_ITEMS,
Paginator::PAGE_SIZE,
$crawler->filter('article.post'),
'The homepage displays the right number of posts.'
);
Expand All @@ -48,7 +49,7 @@ public function testRss(): void
$this->assertResponseHeaderSame('Content-Type', 'text/xml; charset=UTF-8');

$this->assertCount(
Post::NUM_ITEMS,
Paginator::PAGE_SIZE,
$crawler->filter('item'),
'The xml file displays the right number of posts.'
);
Expand Down

0 comments on commit 0de89e1

Please sign in to comment.