Skip to content

Commit

Permalink
[TASK] Cleanup PostRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminkott committed May 19, 2023
1 parent 68c7bf4 commit f79dab1
Showing 1 changed file with 47 additions and 96 deletions.
143 changes: 47 additions & 96 deletions Classes/Domain/Repository/PostRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\RootlineUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Extbase\Persistence\Generic\Qom\ComparisonInterface;
use TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings;
Expand All @@ -34,22 +33,23 @@

class PostRepository extends Repository
{
/**
* @var array
*/
protected $defaultConstraints = [];
protected array $settings = [];
protected array $defaultConstraints = [];

/**
* @throws \Exception
*/
public function initializeObject(): void
{
$querySettings = $this->objectManager->get(Typo3QuerySettings::class);
// don't add the pid constraint
$configurationManager = GeneralUtility::makeInstance(ConfigurationManagerInterface::class);
$this->settings = $configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK, 'blog');

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

$query = $this->createQuery();
$this->defaultConstraints[] = $query->equals('doktype', Constants::DOKTYPE_BLOG_POST);
if (GeneralUtility::makeInstance(Context::class)->getAspect('language')->getId() === 0) {
$this->defaultConstraints[] = $query->logicalOr([
Expand All @@ -59,20 +59,23 @@ public function initializeObject(): void
} else {
$this->defaultConstraints[] = $query->lessThan('l18n_cfg', 2);
}

$this->defaultOrderings = [
'publish_date' => QueryInterface::ORDER_DESCENDING,
];
}

public function findByUidRespectQuerySettings(int $uid)
public function findByUidRespectQuerySettings(int $uid): ?Post
{
$query = $this->createQuery();
$query->matching($query->equals('uid', $uid));
return $query->execute()->getFirst();
/** @var null|Post */
$result = $query->execute()->getFirst();

return $result;
}

/**
* @param PostRepositoryDemand $repositoryDemand;
* @return Post[]
*/
public function findByRepositoryDemand(PostRepositoryDemand $repositoryDemand): array
Expand All @@ -92,6 +95,7 @@ public function findByRepositoryDemand(PostRepositoryDemand $repositoryDemand):
$categoriesConstraints[] = $query->equals('categories.uid', $category->getUid());
}
$categoriesConjunction = $repositoryDemand->getCategoriesConjunction() === Constants::REPOSITORY_CONJUNCTION_AND ? 'logicalAnd' : 'logicalOr';
/** @phpstan-ignore-next-line */
$constraints[] = $query->{$categoriesConjunction}($categoriesConstraints);
}
if ($repositoryDemand->getTags() !== []) {
Expand All @@ -100,6 +104,7 @@ public function findByRepositoryDemand(PostRepositoryDemand $repositoryDemand):
$tagsConstraints[] = $query->equals('tags.uid', $tag->getUid());
}
$tagsConjunction = $repositoryDemand->getTagsConjunction() === Constants::REPOSITORY_CONJUNCTION_AND ? 'logicalAnd' : 'logicalOr';
/** @phpstan-ignore-next-line */
$constraints[] = $query->{$tagsConjunction}($tagsConstraints);
}
if (($ordering = $repositoryDemand->getOrdering()) !== []) {
Expand Down Expand Up @@ -132,41 +137,33 @@ public function findByRepositoryDemand(PostRepositoryDemand $repositoryDemand):

/**
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
* @throws \TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
*/
public function findAll()
{
return $this->getFindAllQuery()->execute();
}

/**
* @param int $blogSetup
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
* @throws \TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
*/
public function findAllByPid(int $blogSetup = null)
public function findAllByPid(?int $blogSetup = null)
{
$query = $this->getFindAllQuery();

if ($blogSetup !== null) {
$existingConstraint = $query->getConstraint();
$additionalConstraint = $query->equals('pid', $blogSetup);
$query->matching($query->logicalAnd([
$existingConstraint,
$additionalConstraint
]));
$constraints = [];
if ($query->getConstraint() !== null) {
$constraints[] = $query->getConstraint();
}
$constraints[] = $query->equals('pid', $blogSetup);
$query->matching($query->logicalAnd($constraints));
}

return $query->execute();
}

/**
* @param int $limit
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
* @throws \TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException#
*/
public function findAllWithLimit(int $limit)
{
Expand All @@ -176,12 +173,6 @@ public function findAllWithLimit(int $limit)
return $query->execute();
}

/**
* @return QueryInterface
* @throws \TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
* @throws \TYPO3\CMS\Core\Context\Exception\AspectNotFoundException
*/
protected function getFindAllQuery(): QueryInterface
{
$query = $this->createQuery();
Expand All @@ -201,10 +192,7 @@ protected function getFindAllQuery(): QueryInterface
}

/**
* @param Author $author
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
* @throws \TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
*/
public function findAllByAuthor(Author $author)
{
Expand All @@ -220,10 +208,7 @@ public function findAllByAuthor(Author $author)
}

/**
* @param Category $category
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
* @throws \TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
*/
public function findAllByCategory(Category $category)
{
Expand All @@ -239,10 +224,7 @@ public function findAllByCategory(Category $category)
}

/**
* @param Tag $tag
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
* @throws \TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
*/
public function findAllByTag(Tag $tag)
{
Expand All @@ -258,12 +240,7 @@ public function findAllByTag(Tag $tag)
}

/**
* @param int $year
* @param int $month
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
* @throws \TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
* @throws \Exception
*/
public function findByMonthAndYear(int $year, int $month = null)
{
Expand All @@ -287,23 +264,18 @@ public function findByMonthAndYear(int $year, int $month = null)
return $query->matching($query->logicalAnd($constraints))->execute();
}

/**
* @return Post
* @throws \TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
*/
public function findCurrentPost(): ?Post
{
$typoScriptFrontendController = $this->getTypoScriptFrontendController();
$pageId = $typoScriptFrontendController
? (int)$typoScriptFrontendController->id
: (int)GeneralUtility::_GP('id');
if ($typoScriptFrontendController === null) {
return null;
}

$currentLanguageId = (int)GeneralUtility::makeInstance(Context::class)
->getPropertyFromAspect('language', 'id', 0);
$pageId = (int) $typoScriptFrontendController->id;
$currentLanguageId = GeneralUtility::makeInstance(Context::class)
->getPropertyFromAspect('language', 'id', 0);

$post = $this->getPostWithLanguage($pageId, $currentLanguageId);

if ($post !== null) {
return $post;
}
Expand All @@ -323,21 +295,19 @@ protected function getPostWithLanguage(int $pageId, int $languageId): ?Post
$constraints[] = $query->equals('uid', $pageId);
}

return $query
/** @var null|Post */
$result = $query
->matching($query->logicalAnd($constraints))
->execute()
->getFirst();

return $result;
}

/**
* @param int $pageId the uid of the page for which fallback languages should be resolved
* @param int $currentLanguageId the requested language, for which fallback languages should be resolved
* @return Post|null
*/
protected function applyLanguageFallback(int $pageId, int $currentLanguageId): ?Post
{
$currentSite = $this->getCurrentSite();
if ($currentSite) {
if ($currentSite !== null) {
/** @var SiteLanguage $languageConfiguration */
$languageConfiguration = $currentSite->getAllLanguages()[$currentLanguageId];
// check the whole language-fallback chain
Expand All @@ -361,12 +331,6 @@ protected function getCurrentSite(): ?Site
return null;
}

/**
* Get month and years with posts.
*
* @return array
* @throws \TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException
*/
public function findMonthsAndYearsWithPosts(): array
{
$query = $this->createQuery();
Expand Down Expand Up @@ -405,12 +369,7 @@ public function findMonthsAndYearsWithPosts(): array
}

/**
* @param int $categoryMultiplier
* @param int $tagMultiplier
* @param int $limit
* @return ObjectStorage
* @throws \TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
* @return ObjectStorage<Post>
*/
public function findRelatedPosts(int $categoryMultiplier = 1, int $tagMultiplier = 1, int $limit = 5): ObjectStorage
{
Expand All @@ -431,10 +390,10 @@ public function findRelatedPosts(int $categoryMultiplier = 1, int $tagMultiplier
continue;
}

if (!array_key_exists($postOfCategory->getUid(), $selectedPosts)) {
$selectedPosts[$postOfCategory->getUid()] = $categoryMultiplier;
if (!array_key_exists((int) $postOfCategory->getUid(), $selectedPosts)) {
$selectedPosts[(int) $postOfCategory->getUid()] = $categoryMultiplier;
} else {
$selectedPosts[$postOfCategory->getUid()] += $categoryMultiplier;
$selectedPosts[(int) $postOfCategory->getUid()] += $categoryMultiplier;
}
}
}
Expand All @@ -447,10 +406,10 @@ public function findRelatedPosts(int $categoryMultiplier = 1, int $tagMultiplier
continue;
}

if (!array_key_exists($postOfTag->getUid(), $selectedPosts)) {
$selectedPosts[$postOfTag->getUid()] = $tagMultiplier;
if (!array_key_exists((int) $postOfTag->getUid(), $selectedPosts)) {
$selectedPosts[(int) $postOfTag->getUid()] = $tagMultiplier;
} else {
$selectedPosts[$postOfTag->getUid()] += $tagMultiplier;
$selectedPosts[(int) $postOfTag->getUid()] += $tagMultiplier;
}
}
}
Expand All @@ -469,16 +428,9 @@ public function findRelatedPosts(int $categoryMultiplier = 1, int $tagMultiplier
return $posts;
}

/**
* @return array
* @throws \TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException
*/
protected function getStoragePidsFromTypoScript(): array
{
$configurationManager = $this->objectManager->get(ConfigurationManager::class);
$settings = $configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);

return GeneralUtility::intExplode(',', $settings['persistence']['storagePid']);
return GeneralUtility::intExplode(',', $this->settings['persistence']['storagePid']);
}

/**
Expand All @@ -497,7 +449,6 @@ protected function getStoragePidConstraint(): ?ComparisonInterface
}

/**
* @return array
* @throws \TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException
*/
protected function getPidsForConstraints(): array
Expand All @@ -507,10 +458,10 @@ protected function getPidsForConstraints(): array
return $value !== '' && (int) $value !== 0;
});

if (count($pids) === 0) {
if (count($pids) === 0 && $this->getTypoScriptFrontendController() !== null) {
$rootLine = GeneralUtility::makeInstance(RootlineUtility::class, $this->getTypoScriptFrontendController()->id)->get();
foreach ($rootLine as $value) {
$pids[] = $value['uid'];
$pids[] = (int) $value['uid'];
}
}

Expand Down

0 comments on commit f79dab1

Please sign in to comment.