Skip to content

Commit

Permalink
+ abstract class PostContentRepository as base class of classes `(S…
Browse files Browse the repository at this point in the history
…ub)?ReplyContentRepository`

* rename abstract `getPosts()` to `getPostsContent()` @ `PostContentRepository`
@ `Post\Content`

* move `__construct()`, `getFid()` & abstract `getTableNameSuffix()` from abstract class `Post\PostRepository` into a new abstract class `RepositoryWithSplitFid`
@ `App\Repository`
@ be
  • Loading branch information
n0099 committed Oct 11, 2024
1 parent 9aea162 commit a52de61
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 36 deletions.
4 changes: 2 additions & 2 deletions be/src/PostsQuery/BaseQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,12 @@ static function (string $postIDName) use ($result): array {

$this->stopwatch->start('parsePostContentProtoBufBytes');
// not using one-to-one association due to relying on PostRepository->getTableNameSuffix()
$replyContents = collect($this->postRepositoryFactory->newReplyContent($fid)->getPosts($allRepliesId))
$replyContents = collect($this->postRepositoryFactory->newReplyContent($fid)->getPostsContent($allRepliesId))
->mapWithKeys(fn(ReplyContent $content) => [$content->getPid() => $content->getContent()]);
$replies->each(fn(Reply $reply) =>
$reply->setContent($replyContents->get($reply->getPid())));

$subReplyContents = collect($this->postRepositoryFactory->newSubReplyContent($fid)->getPosts($spids))
$subReplyContents = collect($this->postRepositoryFactory->newSubReplyContent($fid)->getPostsContent($spids))
->mapWithKeys(fn(SubReplyContent $content) => [$content->getSpid() => $content->getContent()]);
$subReplies->each(fn(SubReply $subReply) =>
$subReply->setContent($subReplyContents->get($subReply->getSpid())));
Expand Down
15 changes: 15 additions & 0 deletions be/src/Repository/Post/Content/PostContentRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App\Repository\Post\Content;

use App\Entity\Post\Content\PostContent;
use App\Repository\RepositoryWithSplitFid;

/**
* @template T of PostContent
* @extends RepositoryWithSplitFid<T>
*/
abstract class PostContentRepository extends RepositoryWithSplitFid
{
abstract public function getPostsContent(\ArrayAccess $postsId): array;
}
7 changes: 3 additions & 4 deletions be/src/Repository/Post/Content/ReplyContentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
namespace App\Repository\Post\Content;

use App\Entity\Post\Content\ReplyContent;
use App\Repository\Post\PostRepository;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\DependencyInjection\Attribute\Exclude;

/**
* @extends PostRepository<ReplyContent>
* @extends PostContentRepository<ReplyContent>
*/
#[Exclude]
class ReplyContentRepository extends PostRepository
class ReplyContentRepository extends PostContentRepository
{
public function __construct(ManagerRegistry $registry, EntityManagerInterface $entityManager, int $fid)
{
Expand All @@ -24,7 +23,7 @@ protected function getTableNameSuffix(): string
return 'reply_content';
}

public function getPosts(\ArrayAccess $postsId): array
public function getPostsContent(\ArrayAccess $postsId): array
{
$dql = 'SELECT t FROM App\Entity\Post\Content\ReplyContent t WHERE t.pid IN (:pid)';
return $this->getQueryResultWithSingleParam($dql, 'pid', $postsId);
Expand Down
4 changes: 2 additions & 2 deletions be/src/Repository/Post/Content/SubReplyContentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @extends PostRepository<SubReplyContent>
*/
#[Exclude]
class SubReplyContentRepository extends PostRepository
class SubReplyContentRepository extends PostContentRepository
{
public function __construct(ManagerRegistry $registry, EntityManagerInterface $entityManager, int $fid)
{
Expand All @@ -24,7 +24,7 @@ protected function getTableNameSuffix(): string
return 'subReply_content';
}

public function getPosts(\ArrayAccess $postsId): array
public function getPostsContent(\ArrayAccess $postsId): array
{
$dql = 'SELECT t FROM App\Entity\Post\Content\SubReplyContent t WHERE t.spid IN (:spid)';
return $this->getQueryResultWithSingleParam($dql, 'spid', $postsId);
Expand Down
31 changes: 3 additions & 28 deletions be/src/Repository/Post/PostRepository.php
Original file line number Diff line number Diff line change
@@ -1,43 +1,18 @@
<?php


namespace App\Repository\Post;

use App\Entity\Post\Post;
use App\Helper;
use App\Repository\BaseRepository;
use Doctrine\ORM\EntityManagerInterface;
use App\Repository\RepositoryWithSplitFid;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ManagerRegistry;

/**
* @template T of Post
* @extends BaseRepository<T>
* @extends RepositoryWithSplitFid<T>
*/
abstract class PostRepository extends BaseRepository
abstract class PostRepository extends RepositoryWithSplitFid
{
abstract protected function getTableNameSuffix(): string;

/**
* @param class-string<T> $postRepositoryClass
*/
public function __construct(
ManagerRegistry $registry,
EntityManagerInterface $entityManager,
string $postRepositoryClass,
private readonly int $fid,
) {
parent::__construct($registry, $postRepositoryClass);
$entityManager->getClassMetadata($postRepositoryClass)->setPrimaryTable([
'name' => "\"tbmc_f{$fid}_" . $this->getTableNameSuffix() . '"',
]);
}

public function getFid(): int
{
return $this->fid;
}

public function selectCurrentAndParentPostID(): QueryBuilder
{
return $this->createQueryBuilder('t')->select(collect(Helper::POST_TYPE_TO_ID)
Expand Down
35 changes: 35 additions & 0 deletions be/src/Repository/RepositoryWithSplitFid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Repository;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;

/**
* @template T
* @extends BaseRepository<T>
*/
abstract class RepositoryWithSplitFid extends BaseRepository
{
abstract protected function getTableNameSuffix(): string;

/**
* @param class-string<T> $postRepositoryClass
*/
public function __construct(
ManagerRegistry $registry,
EntityManagerInterface $entityManager,
string $postRepositoryClass,
private readonly int $fid,
) {
parent::__construct($registry, $postRepositoryClass);
$entityManager->getClassMetadata($postRepositoryClass)->setPrimaryTable([
'name' => "\"tbmc_f{$fid}_" . $this->getTableNameSuffix() . '"',
]);
}

public function getFid(): int
{
return $this->fid;
}
}

0 comments on commit a52de61

Please sign in to comment.