Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…table-name-for-entity/79084344#79084344 after previous attempt 18f1b9c @ config/packages/doctrine.yaml

* mark entire class and derived classes as `readonly` so all props including ones outside the construct promotion are now readonly
+ `setOrderBy(Field|Desc)()` to allow derived classes init its namesake readonly props instead of directly assigning them
* narrow the type of key in `Collection<,>` for variable `$paginators` and param `$queries` @ `setResult()`
@ `App\PostsQuery\BaseQuery`
@ be
  • Loading branch information
n0099 committed Oct 13, 2024
1 parent 59b547b commit 22e8b8a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 28 deletions.
9 changes: 7 additions & 2 deletions be/config/packages/doctrine.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ doctrine:
alias: DTO
controller_resolver:
auto_mapping: false
query_cache_driver:
type: service
id: cache.adapter.null

services:
cache.adapter.null:
class: Symfony\Component\Cache\Adapter\NullAdapter

when@test:
doctrine:
Expand All @@ -52,5 +59,3 @@ when@prod:
pools:
doctrine.result_cache_pool:
adapter: cache.app
doctrine.system_cache_pool:
adapter: cache.system
32 changes: 21 additions & 11 deletions be/src/PostsQuery/BaseQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use Symfony\Component\Stopwatch\Stopwatch;

/** @psalm-import-type PostsKeyByTypePluralName from CursorCodec */
abstract class BaseQuery
abstract readonly class BaseQuery

Check warning on line 23 in be/src/PostsQuery/BaseQuery.php

View workflow job for this annotation

GitHub Actions / runs-on (windows-latest) / phpmd

BaseQuery.php: The class BaseQuery has a coupling between objects value of 17. Consider to reduce the number of dependencies under 13. (CouplingBetweenObjects, Design Rules)
{
/** @type array{
* fid: int,
Expand All @@ -37,24 +37,34 @@ abstract class BaseQuery

protected bool $orderByDesc;

abstract public function query(QueryParams $params, ?string $cursor): void;

public function __construct(
private readonly NormalizerInterface $normalizer,
private readonly Stopwatch $stopwatch,
private readonly CursorCodec $cursorCodec,
private readonly PostRepositoryFactory $postRepositoryFactory,
private readonly int $perPageItems = 50,
private NormalizerInterface $normalizer,
private Stopwatch $stopwatch,
private CursorCodec $cursorCodec,
private PostRepositoryFactory $postRepositoryFactory,
private int $perPageItems = 50,
) {}

abstract public function query(QueryParams $params, ?string $cursor): void;

public function getResultPages(): array
{
return $this->queryResultPages;
}

protected function setOrderByField(string $orderByField): void
{
$this->orderByField = $orderByField;
}

protected function setOrderByDesc(bool $orderByDesc): void
{
$this->orderByDesc = $orderByDesc;
}

/**
* @param int $fid
* @param Collection<string, QueryBuilder> $queries key by post type
* @param Collection<Helper::POST_TYPE, QueryBuilder> $queries key by post type
* @param string|null $cursorParamValue
* @param string|null $queryByPostIDParamName
* @return void
Expand All @@ -67,7 +77,7 @@ protected function setResult(
): void {
$this->stopwatch->start('setResult');

$orderedQueries = $queries->map(fn(QueryBuilder $qb, string $postType): QueryBuilder => $qb
$orderedQueries = $queries->each(fn(QueryBuilder $qb, string $postType): QueryBuilder => $qb
->addOrderBy("t.$this->orderByField", $this->orderByDesc === true ? 'DESC' : 'ASC')
// cursor paginator requires values of orderBy column are unique
// if not it should fall back to other unique field (here is the post ID primary key)
Expand All @@ -82,7 +92,7 @@ protected function setResult(
// remove queries for post types with encoded cursor ',,'
$orderedQueries = $orderedQueries->intersectByKeys($cursorsKeyByPostType);
}
/** @var Collection<string, QueryBuilder> $paginators key by post type */
/** @var Collection<Helper::POST_TYPE, QueryBuilder> $paginators key by post type */
$paginators = $orderedQueries->each(function (QueryBuilder $qb, string $type) use ($cursorsKeyByPostType) {
$cursors = $cursorsKeyByPostType?->get($type);
if ($cursors === null) {
Expand Down
12 changes: 6 additions & 6 deletions be/src/PostsQuery/IndexQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Stopwatch\Stopwatch;

class IndexQuery extends BaseQuery
readonly class IndexQuery extends BaseQuery
{
public function __construct(
NormalizerInterface $normalizer,
Stopwatch $stopwatch,
CursorCodec $cursorCodec,
private readonly PostRepositoryFactory $postRepositoryFactory,
private readonly ForumRepository $forumRepository,
private PostRepositoryFactory $postRepositoryFactory,
private ForumRepository $forumRepository,
) {
parent::__construct($normalizer, $stopwatch, $cursorCodec, $postRepositoryFactory);
}
Expand All @@ -42,11 +42,11 @@ public function query(QueryParams $params, ?string $cursor): void
$postTypes = $flatParams['postTypes'];

if ($flatParams['orderBy'] === 'default') {
$this->orderByField = 'postedAt'; // order by postedAt to prevent posts out of order when order by post ID
$this->setOrderByField('postedAt'); // order by postedAt to prevent posts out of order when order by post ID
if (\array_key_exists('fid', $flatParams) && $postIDParam->count() === 0) { // query by fid only
$this->orderByDesc = true;
$this->setOrderByDesc(true);
} elseif ($hasPostIDParam) { // query by post ID (with or without fid)
$this->orderByDesc = false;
$this->setOrderByDesc(false);
}
}

Expand Down
14 changes: 5 additions & 9 deletions be/src/PostsQuery/SearchQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Stopwatch\Stopwatch;

class SearchQuery extends BaseQuery
readonly class SearchQuery extends BaseQuery
{
public function __construct(
NormalizerInterface $normalizer,
Stopwatch $stopwatch,
CursorCodec $cursorCodec,
private readonly PostRepositoryFactory $postRepositoryFactory,
private readonly UserRepository $userRepository,
private PostRepositoryFactory $postRepositoryFactory,
private UserRepository $userRepository,
) {
parent::__construct($normalizer, $stopwatch, $cursorCodec, $postRepositoryFactory);
}
Expand All @@ -28,12 +28,8 @@ public function query(QueryParams $params, ?string $cursor): void
$fid = $params->getUniqueParamValue('fid');

$orderByParam = $params->pick('orderBy')[0];
$this->orderByField = $orderByParam->value;
$this->orderByDesc = $orderByParam->getSub('direction');
if ($this->orderByField === 'default') {
$this->orderByField = 'postedAt';
$this->orderByDesc = true;
}
$this->setOrderByField($orderByParam->value === 'default' ? 'postedAt' : $orderByParam->value);
$this->setOrderByDesc($orderByParam->value === 'default' ? true : $orderByParam->getSub('direction'));

/** @var array<string, array> $cachedUserQueryResult key by param name */
$cachedUserQueryResult = [];
Expand Down

0 comments on commit 22e8b8a

Please sign in to comment.