Skip to content

Commit

Permalink
[TASK] Cleanup CommentRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminkott committed May 19, 2023
1 parent c810437 commit 9135697
Showing 1 changed file with 24 additions and 48 deletions.
72 changes: 24 additions & 48 deletions Classes/Domain/Repository/CommentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,18 @@

class CommentRepository extends Repository
{
/**
* @var ConfigurationManagerInterface
*/
protected $configurationManager;

/**
* @var array
*/
protected $settings;
protected array $settings = [];

/**
* @throws \InvalidArgumentException
*/
public function initializeObject(): void
{
$this->configurationManager = $this->objectManager->get(ConfigurationManagerInterface::class);
$this->settings = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS, 'blog');

$querySettings = $this->objectManager->get(Typo3QuerySettings::class);
// don't add the pid constraint
$configurationManager = GeneralUtility::makeInstance(ConfigurationManagerInterface::class);
$this->settings = $configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS, 'blog');

$querySettings = GeneralUtility::makeInstance(
Typo3QuerySettings::class,
GeneralUtility::makeInstance(Context::class),
$configurationManager
);
$querySettings->setRespectStoragePage(false);
$this->setDefaultQuerySettings($querySettings);

Expand All @@ -52,30 +44,27 @@ public function initializeObject(): void
}

/**
* @param Post $post
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
* @throws \TYPO3\CMS\Core\Context\Exception\AspectNotFoundException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
*/
public function findAllByPost(Post $post)
{
$query = $this->createQuery();
$constraints = [];
$constraints[] = $query->equals('post', $post->getUid());
$constraints = $this->fillConstraintsBySettings($query, $constraints);
return $query->matching($query->logicalAnd($constraints))->execute();
$statement = $query->matching($query->logicalAnd(...$constraints));
$result = $statement->execute();

return $result;
}

/**
* @param string $filter
* @param int $blogSetup
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
*/
public function findAllByFilter(string $filter = null, int $blogSetup = null)
{
$query = $this->createQuery();
$querySettings = $this->objectManager->get(Typo3QuerySettings::class);
$querySettings = $query->getQuerySettings();
$querySettings->setRespectStoragePage(false);
$query->setQuerySettings($querySettings);

Expand Down Expand Up @@ -103,18 +92,14 @@ public function findAllByFilter(string $filter = null, int $blogSetup = null)
$constraints[] = $query->in('pid', $this->getPostPidsByRootPid($blogSetup));
}
if (count($constraints) > 0) {
return $query->matching($query->logicalAnd($constraints))->execute();
return $query->matching($query->logicalAnd(...$constraints))->execute();
}

return $this->findAll();
}

/**
* @param int $limit
* @param int $blogSetup
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
* @throws \TYPO3\CMS\Core\Context\Exception\AspectNotFoundException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
*/
public function findActiveComments(int $limit = null, int $blogSetup = null)
{
Expand All @@ -132,13 +117,10 @@ public function findActiveComments(int $limit = null, int $blogSetup = null)
$constraints[] = $query->in('pid', $storagePids);
}
}
return $query->matching($query->logicalAnd($constraints))->execute();

return $query->matching($query->logicalAnd(...$constraints))->execute();
}

/**
* @param int $blogRootPid
* @return array
*/
protected function getPostPidsByRootPid(int $blogRootPid): array
{
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
Expand All @@ -148,8 +130,8 @@ protected function getPostPidsByRootPid(int $blogRootPid): array
->from('pages')
->where($queryBuilder->expr()->eq('doktype', Constants::DOKTYPE_BLOG_POST))
->andWhere($queryBuilder->expr()->eq('pid', $blogRootPid))
->execute()
->fetchAll();
->executeQuery()
->fetchAllAssociative();
$result = [];
foreach ($rows as $row) {
$result[] = $row['uid'];
Expand All @@ -158,13 +140,6 @@ protected function getPostPidsByRootPid(int $blogRootPid): array
return $result;
}

/**
* @param QueryInterface $query
* @param array $constraints
* @return array
* @throws \TYPO3\CMS\Core\Context\Exception\AspectNotFoundException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
*/
public function fillConstraintsBySettings(QueryInterface $query, array $constraints): array
{
$respectCommentsModeration = isset($this->settings['comments']['moderation'])
Expand All @@ -177,8 +152,8 @@ public function fillConstraintsBySettings(QueryInterface $query, array $constrai
}

$respectPostLanguageId = isset($this->settings['comments']['respectPostLanguageId'])
? (int)$this->settings['comments']['respectPostLanguageId']
: 0;
? (bool) $this->settings['comments']['respectPostLanguageId']
: false;
if ($respectPostLanguageId) {
$constraints[] = $query->logicalOr([
$query->equals('postLanguageId', GeneralUtility::makeInstance(Context::class)->getAspect('language')->getId()),
Expand All @@ -190,17 +165,18 @@ public function fillConstraintsBySettings(QueryInterface $query, array $constrai
$constraints[] = $query->logicalAnd([
$query->logicalOr([
$query->equals('post.starttime', 0),
$query->lessThanOrEqual('post.starttime', $tstamp)
$query->lessThanOrEqual('post.starttime', $tstamp),
]),
$query->logicalOr([
$query->equals('post.endtime', 0),
$query->greaterThanOrEqual('post.endtime', $tstamp)
$query->greaterThanOrEqual('post.endtime', $tstamp),
])
]);
$constraints[] = $query->logicalAnd([
$query->equals('post.hidden', 0),
$query->equals('post.deleted', 0)
]);

return $constraints;
}
}

0 comments on commit 9135697

Please sign in to comment.