Skip to content

Commit

Permalink
[TASK] Clean up some linkvalidator classes
Browse files Browse the repository at this point in the history
Minor class modification to make the code more
dense, more strict and reduce duplication.

Resolves: #99094
Releases: main
Change-Id: Iac3e7926dd16351ae0e0e7c45dfd099aa2eb61b6
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/76608
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: core-ci <typo3@b13.com>
Tested-by: Jochen <rothjochen@gmail.com>
Reviewed-by: Jochen <rothjochen@gmail.com>
Tested-by: Stefan Bürk <stefan@buerk.tech>
Reviewed-by: Stefan Bürk <stefan@buerk.tech>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
  • Loading branch information
lolli42 committed Nov 15, 2022
1 parent b79b488 commit 8d18d9e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 95 deletions.
2 changes: 1 addition & 1 deletion typo3/sysext/linkvalidator/Classes/LinkAnalyzer.php
Expand Up @@ -449,7 +449,7 @@ protected function analyzeTypoLinks(SoftReferenceParserResult $parserResult, arr
*
* @return array array with the number of links found
*/
public function getLinkCounts()
public function getLinkCounts(): array
{
return $this->brokenLinkRepository->getNumberOfBrokenLinksForRecordsOnPages($this->pids, $this->searchFields);
}
Expand Down
76 changes: 10 additions & 66 deletions typo3/sysext/linkvalidator/Classes/Result/LinkAnalyzerResult.php
Expand Up @@ -36,77 +36,28 @@
*/
class LinkAnalyzerResult
{
/**
* @var LinkAnalyzer
*/
protected $linkAnalyzer;

/**
* @var BrokenLinkRepository
*/
protected $brokenLinkRepository;

/**
* @var PagesRepository
*/
protected $pagesRepository;

/**
* @var ConnectionPool
*/
protected $connectionPool;

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

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

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

/**
* @var bool
*/
protected $differentToLastResult = false;
protected array $brokenLinks = [];
protected array $newBrokenLinkCounts = [];
protected array $oldBrokenLinkCounts = [];
protected bool $differentToLastResult = false;

/**
* Save localized pages to reduce database requests
*
* @var array<string, int>
*/
protected $localizedPages = [];
protected array $localizedPages = [];

public function __construct(
LinkAnalyzer $linkAnalyzer,
BrokenLinkRepository $brokenLinkRepository,
ConnectionPool $connectionPool,
PagesRepository $pagesRepository
private readonly LinkAnalyzer $linkAnalyzer,
private readonly BrokenLinkRepository $brokenLinkRepository,
private readonly ConnectionPool $connectionPool,
private readonly PagesRepository $pagesRepository
) {
$this->linkAnalyzer = $linkAnalyzer;
$this->brokenLinkRepository = $brokenLinkRepository;
$this->connectionPool = $connectionPool;
$this->pagesRepository = $pagesRepository;
}

/**
* Call LinkAnalyzer with provided task configuration and process result values
*
* @param int $page
* @param int $depth
* @param array $pageRow
* @param array $modTSconfig
* @param array $searchFields
* @param array $linkTypes
* @param string $languages
*
* @return $this
*/
public function getResultForTask(
int $page,
Expand Down Expand Up @@ -213,7 +164,6 @@ public function isDifferentToLastResult(): bool
* Process the link counts (old and new) and ensures that all link types are available in the array
*
* @param array<int, string> $linkTypes list of link types
* @return LinkAnalyzerResult
*/
protected function processLinkCounts(array $linkTypes): self
{
Expand All @@ -236,12 +186,10 @@ protected function processLinkCounts(array $linkTypes): self
* Process broken link values and assign them to new variables which are used in the templates
* shipped by the core but can also be used in custom templates. The raw data is untouched and
* can also still be used in custom templates.
*
* @return LinkAnalyzerResult
*/
protected function processBrokenLinks(): self
{
foreach ($this->brokenLinks as $key => &$brokenLink) {
foreach ($this->brokenLinks as &$brokenLink) {
$fullRecord = BackendUtility::getRecord($brokenLink['table_name'], $brokenLink['record_uid']);

if ($fullRecord !== null) {
Expand Down Expand Up @@ -274,10 +222,6 @@ protected function processBrokenLinks(): self

/**
* Get localized page id and store it in the local property localizedPages
*
* @param int $parentId
* @param int $languageId
* @return int
*/
protected function getLocalizedPageId(int $parentId, int $languageId): int
{
Expand Down
32 changes: 4 additions & 28 deletions typo3/sysext/linkvalidator/Classes/Task/ValidatorTask.php
Expand Up @@ -134,7 +134,6 @@ public function getEmail(): string
* Set the value of the private property email.
*
* @param string $email Email address to which an email report is sent
* @return ValidatorTask
*/
public function setEmail(string $email): self
{
Expand All @@ -156,7 +155,6 @@ public function getEmailOnBrokenLinkOnly(): bool
* Set the value of the private property emailOnBrokenLinkOnly
*
* @param bool $emailOnBrokenLinkOnly Only send an email, if new broken links were found
* @return ValidatorTask
*/
public function setEmailOnBrokenLinkOnly(bool $emailOnBrokenLinkOnly): self
{
Expand All @@ -178,7 +176,6 @@ public function getPage(): int
* Set the value of the private property page
*
* @param int $page UID of the start page for this task.
* @return ValidatorTask
*/
public function setPage(int $page): self
{
Expand All @@ -200,7 +197,6 @@ public function getLanguages(): string
* Set the value of the private property languages
*
* @param string $languages Languages to fetch broken links
* @return ValidatorTask
*/
public function setLanguages(string $languages): self
{
Expand All @@ -222,7 +218,6 @@ public function getDepth(): int
* Set the value of the private property depth
*
* @param int $depth Level of pages the task should check
* @return ValidatorTask
*/
public function setDepth(int $depth): self
{
Expand All @@ -244,7 +239,6 @@ public function getEmailTemplateName(): string
* Set the value of the private property emailTemplateName
*
* @param string $emailTemplateName Template name to be used for the email
* @return ValidatorTask
*/
public function setEmailTemplateName(string $emailTemplateName): self
{
Expand All @@ -266,7 +260,6 @@ public function getConfiguration(): string
* Set the value of the private property configuration
*
* @param string $configuration specific TSconfig for this task
* @return ValidatorTask
*/
public function setConfiguration(string $configuration): self
{
Expand All @@ -285,9 +278,7 @@ public function execute(): bool
return false;
}

$this
->setCliArguments()
->loadModTSconfig();
$this->setCliArguments()->loadModTSconfig();

$successfullyExecuted = true;
$linkAnalyzerResult = $this->getLinkAnalyzerResult();
Expand All @@ -309,8 +300,6 @@ public function execute(): bool
/**
* Validate all broken links for pages set in the task configuration
* and return the analyzers result as object.
*
* @return LinkAnalyzerResult
*/
protected function getLinkAnalyzerResult(): LinkAnalyzerResult
{
Expand All @@ -336,10 +325,8 @@ protected function getLinkAnalyzerResult(): LinkAnalyzerResult

/**
* Load and merge linkvalidator TSconfig from task configuration with page TSconfig
*
* @return ValidatorTask
*/
protected function loadModTSconfig(): self
protected function loadModTSconfig(): void
{
$parseObj = GeneralUtility::makeInstance(TypoScriptParser::class);
$parseObj->parse($this->configuration);
Expand All @@ -355,6 +342,7 @@ protected function loadModTSconfig(): self
if (is_array($overrideTs) && $overrideTs !== []) {
ArrayUtility::mergeRecursiveWithOverrule($modTs, $overrideTs);
}

if (empty($modTs['mail.']['fromemail'])) {
$modTs['mail.']['fromemail'] = MailUtility::getSystemFromAddress();
}
Expand All @@ -363,8 +351,6 @@ protected function loadModTSconfig(): self
}

$this->modTSconfig = $modTs;

return $this;
}

/**
Expand Down Expand Up @@ -404,8 +390,7 @@ protected function getLinkTypes(): array
/**
* Build and send report email when broken links were found
*
* @param LinkAnalyzerResult $linkAnalyzerResult
* @return bool TRUE if mail was sent, FALSE if or not
* @return bool TRUE if mail was sent, FALSE if not
*/
protected function reportEmail(LinkAnalyzerResult $linkAnalyzerResult): bool
{
Expand Down Expand Up @@ -478,8 +463,6 @@ protected function reportEmail(LinkAnalyzerResult $linkAnalyzerResult): bool
/**
* Returns the most important properties of the LinkValidator task as a
* comma separated string that will be displayed in the scheduler module.
*
* @return string
*/
public function getAdditionalInformation(): string
{
Expand All @@ -505,8 +488,6 @@ public function getAdditionalInformation(): string

/**
* Simulate cli call with setting the required options to the $_SERVER['argv']
*
* @return ValidatorTask
*/
protected function setCliArguments(): self
{
Expand All @@ -528,8 +509,6 @@ protected function setCliArguments(): self

/**
* Get FluidEmail with template from the task configuration
*
* @return FluidEmail
*/
protected function getFluidEmail(): FluidEmail
{
Expand Down Expand Up @@ -557,9 +536,6 @@ protected function getFluidEmail(): FluidEmail

/**
* Check if both template files (html and txt) exist under at least one template path
*
* @param array $templatePaths
* @return bool
*/
protected function templateFilesExist(array $templatePaths): bool
{
Expand Down

0 comments on commit 8d18d9e

Please sign in to comment.