Skip to content

Commit

Permalink
ProcessInstructionService (#884)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasnorre authored Mar 8, 2024
1 parent ff41a1e commit 4c7d37e
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
### Deprecated
#### Classes
#### Functions & Properties
* CrawlerController->drawURLs_PIfilter()

### Removed
* Obsolete columns (first and last time) from Process Overview
Expand Down
20 changes: 9 additions & 11 deletions Classes/Controller/CrawlerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use AOE\Crawler\QueueExecutor;
use AOE\Crawler\Service\ConfigurationService;
use AOE\Crawler\Service\PageService;
use AOE\Crawler\Service\ProcessInstructionService;
use AOE\Crawler\Service\UrlService;
use AOE\Crawler\Value\QueueRow;
use PDO;
Expand Down Expand Up @@ -226,9 +227,13 @@ public function urlListFromUrlArray(
);

$urlService = new UrlService();
$processInstructionService = new ProcessInstructionService();

foreach ($vv['URLs'] as $urlQuery) {
if (! $this->drawURLs_PIfilter($vv['subCfg']['procInstrFilter'] ?? '', $incomingProcInstructions)) {
if (! $processInstructionService->isAllowed(
$vv['subCfg']['procInstrFilter'] ?? '',
$incomingProcInstructions
)) {
continue;
}
$url = $urlService->getUrlFromPageAndQueryParameters(
Expand Down Expand Up @@ -289,19 +294,12 @@ public function urlListFromUrlArray(
* @param string $piString PI to test
* @param array $incomingProcInstructions Processing instructions
* @return boolean
* @deprecated since 11.0.3 will be removed in v13.x
*/
public function drawURLs_PIfilter(string $piString, array $incomingProcInstructions)
{
if (empty($incomingProcInstructions)) {
return true;
}

foreach ($incomingProcInstructions as $pi) {
if (GeneralUtility::inList($piString, $pi)) {
return true;
}
}
return false;
$processInstructionService = new ProcessInstructionService();
return $processInstructionService->isAllowed($piString, $incomingProcInstructions);
}

public function getPageTSconfigForId(int $id): array
Expand Down
42 changes: 42 additions & 0 deletions Classes/Service/ProcessInstructionService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace AOE\Crawler\Service;

/*
* (c) 2022 Tomas Norre Mikkelsen <[email protected]>
*
* This file is part of the TYPO3 Crawler Extension.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
* @internal since v11.0.3
*/
class ProcessInstructionService
{
public function isAllowed(string $processInstruction, array $incoming): bool
{
if (empty($incoming)) {
return true;
}

foreach ($incoming as $pi) {
if (GeneralUtility::inList($processInstruction, $pi)) {
return true;
}
}
return false;
}
}
76 changes: 76 additions & 0 deletions Tests/Unit/Service/ProcessInstructionServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

namespace AOE\Crawler\Tests\Unit\Service;

/*
* (c) 2022 Tomas Norre Mikkelsen <[email protected]>
*
* This file is part of the TYPO3 Crawler Extension.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

use AOE\Crawler\Service\ProcessInstructionService;

#[\PHPUnit\Framework\Attributes\CoversClass(\AOE\Crawler\Service\ProcessInstructionService::class)]
class ProcessInstructionServiceTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
{
private ProcessInstructionService $processInstructionService;

protected function setUp(): void
{
parent::setUp();
$this->processInstructionService = new ProcessInstructionService();
}

#[\PHPUnit\Framework\Attributes\Test]
#[\PHPUnit\Framework\Attributes\DataProvider('isAllowedDataProvider')]
public function isAllowReturnsExpectedBoolValue(
string $piString,
array $incomingProcInstructions,
bool $expected
): void {
self::assertEquals(
$expected,
$this->processInstructionService->isAllowed($piString, $incomingProcInstructions)
);
}

public static function isAllowedDataProvider(): iterable
{
yield 'Not in list' => [
'piString' => 'tx_indexedsearch_reindex,tx_esetcache_clean_main',
'incomingProcInstructions' => ['tx_unknown_extension_instruction'],
'expected' => false,
];
yield 'In list' => [
'piString' => 'tx_indexedsearch_reindex,tx_esetcache_clean_main',
'incomingProcInstructions' => ['tx_indexedsearch_reindex'],
'expected' => true,
];
yield 'Twice in list' => [
'piString' => 'tx_indexedsearch_reindex,tx_esetcache_clean_main',
'incomingProcInstructions' => ['tx_indexedsearch_reindex', 'tx_indexedsearch_reindex'],
'expected' => true,
];
yield 'Empty incomingProcInstructions' => [
'piString' => '',
'incomingProcInstructions' => [],
'expected' => true,
];
yield 'In list CAPITALIZED' => [
'piString' => 'tx_indexedsearch_reindex,tx_esetcache_clean_main',
'incomingProcInstructions' => ['TX_INDEXEDSEARCH_REINDES'],
'expected' => false,
];
}
}
3 changes: 1 addition & 2 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ parameters:
path: Classes/Controller/CrawlerController.php

-
message: "#^Class cognitive complexity is 101, keep it under 60$#"
message: "#^Class cognitive complexity is 97, keep it under 60$#"
count: 1
path: Classes/Controller/CrawlerController.php

Expand Down Expand Up @@ -212,4 +212,3 @@ parameters:
message: "#^Parameter \\#1 \\$row of static method TYPO3\\\\CMS\\\\Core\\\\Utility\\\\CsvUtility\\:\\:csvValues\\(\\) expects array\\<string\\>, array\\<int, int\\|string\\> given\\.$#"
count: 1
path: Classes/Writer/FileWriter/CsvWriter/CrawlerCsvWriter.php

0 comments on commit 4c7d37e

Please sign in to comment.