Skip to content

Commit

Permalink
[TASK] Add CrawlStrategyFactory to move logic from QueueExecutor
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasnorre authored Oct 12, 2020
1 parent c01e5db commit c9d0889
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 57 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Added
* Documentation example for ext:news
* CrawlStrategyFactory to move login out of the QueueExecutor

### Fixed
* Frontend User initialization with UserGroups for crawling protected pages
Expand Down
4 changes: 3 additions & 1 deletion Classes/Controller/CrawlerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

use AOE\Crawler\Configuration\ExtensionConfigurationProvider;
use AOE\Crawler\Converter\JsonCompatibilityConverter;
use AOE\Crawler\CrawlStrategy\CrawlStrategyFactory;
use AOE\Crawler\Domain\Repository\ConfigurationRepository;
use AOE\Crawler\Domain\Repository\ProcessRepository;
use AOE\Crawler\Domain\Repository\QueueRepository;
Expand Down Expand Up @@ -229,10 +230,11 @@ class CrawlerController implements LoggerAwareInterface
public function __construct()
{
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
$crawlStrategyFactory = GeneralUtility::makeInstance(CrawlStrategyFactory::class);
$this->queueRepository = $objectManager->get(QueueRepository::class);
$this->processRepository = $objectManager->get(ProcessRepository::class);
$this->configurationRepository = $objectManager->get(ConfigurationRepository::class);
$this->queueExecutor = $objectManager->get(QueueExecutor::class);
$this->queueExecutor = GeneralUtility::makeInstance(QueueExecutor::class, $crawlStrategyFactory);
$this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);

$this->processFilename = Environment::getVarPath() . '/lock/tx_crawler.proc';
Expand Down
36 changes: 36 additions & 0 deletions Classes/CrawlStrategy/CrawlStrategyFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace AOE\Crawler\CrawlStrategy;

use AOE\Crawler\Configuration\ExtensionConfigurationProvider;
use TYPO3\CMS\Core\Utility\GeneralUtility;

final class CrawlStrategyFactory
{
/**
* @var ExtensionConfigurationProvider
*/
private $configurationProvider;

public function __construct(?ExtensionConfigurationProvider $configurationProvider = null)
{
$this->configurationProvider = $configurationProvider ?? GeneralUtility::makeInstance(ExtensionConfigurationProvider::class);
}

public function create(): CrawlStrategy
{
$settings = $this->configurationProvider->getExtensionConfiguration();
$extensionSettings = is_array($settings) ? $settings : [];

if ($extensionSettings['makeDirectRequests']) {
/** @var CrawlStrategy $instance */
$instance = GeneralUtility::makeInstance(SubProcessExecutionStrategy::class);
} else {
$instance = GeneralUtility::makeInstance(GuzzleExecutionStrategy::class);
}

return $instance;
}
}
26 changes: 6 additions & 20 deletions Classes/QueueExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@
* The TYPO3 project - inspiring people to share!
*/

use AOE\Crawler\Configuration\ExtensionConfigurationProvider;
use AOE\Crawler\Controller\CrawlerController;
use AOE\Crawler\Converter\JsonCompatibilityConverter;
use AOE\Crawler\CrawlStrategy\CallbackExecutionStrategy;
use AOE\Crawler\CrawlStrategy\GuzzleExecutionStrategy;
use AOE\Crawler\CrawlStrategy\SubProcessExecutionStrategy;
use AOE\Crawler\CrawlStrategy\CrawlStrategyFactory;
use AOE\Crawler\Utility\SignalSlotUtility;
use TYPO3\CMS\Core\Http\Uri;
use TYPO3\CMS\Core\SingletonInterface;
Expand All @@ -36,25 +34,13 @@
class QueueExecutor implements SingletonInterface
{
/**
* @var GuzzleExecutionStrategy|SubProcessExecutionStrategy
* @var CrawlStrategy
*/
protected $selectedStrategy;
protected $crawlStrategy;

/**
* @var array
*/
protected $extensionSettings;

public function __construct(?ExtensionConfigurationProvider $configurationProvider = null)
public function __construct(CrawlStrategyFactory $crawlStrategyFactory)
{
$configurationProvider = $configurationProvider ?? GeneralUtility::makeInstance(ExtensionConfigurationProvider::class);
$settings = $configurationProvider->getExtensionConfiguration();
$this->extensionSettings = is_array($settings) ? $settings : [];
if ($this->extensionSettings['makeDirectRequests']) {
$this->selectedStrategy = GeneralUtility::makeInstance(SubProcessExecutionStrategy::class);
} else {
$this->selectedStrategy = GeneralUtility::makeInstance(GuzzleExecutionStrategy::class);
}
$this->crawlStrategy = $crawlStrategyFactory->create();
}

/**
Expand Down Expand Up @@ -88,7 +74,7 @@ public function executeQueueItem(array $queueItem, CrawlerController $crawlerCon

// Get result:
$url = new Uri($parameters['url']);
$result = $this->selectedStrategy->fetchUrlContents($url, $crawlerId);
$result = $this->crawlStrategy->fetchUrlContents($url, $crawlerId);
if ($result !== false) {
$result = ['content' => json_encode($result)];
}
Expand Down
65 changes: 65 additions & 0 deletions Tests/Unit/CrawlStrategy/CrawlStrategyFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace AOE\Crawler\Tests\Unit\CrawlStrategy;

/*
* (c) 2020 AOE GmbH <dev@aoe.com>
*
* 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\CrawlStrategy\CrawlStrategyFactory;
use AOE\Crawler\CrawlStrategy\GuzzleExecutionStrategy;
use AOE\Crawler\CrawlStrategy\SubProcessExecutionStrategy;
use Nimut\TestingFramework\TestCase\UnitTestCase;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class CrawlStrategyFactoryTest extends UnitTestCase
{
/**
* @test
*/
public function crawlerStrategyFactoryReturnsGuzzleExecutionStrategy(): void
{
$configuration = [
'makeDirectRequests' => 0,
'frontendBasePath' => '/',
];
$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['crawler'] = $configuration;
$crawlStrategy = GeneralUtility::makeInstance(CrawlStrategyFactory::class)->create();

self::assertInstanceOf(
GuzzleExecutionStrategy::class,
$crawlStrategy
);
}

/**
* @test
*/
public function crawlerStrategyFactoryReturnsSubProcessExecutionStrategy(): void
{
$configuration = [
'makeDirectRequests' => 1,
'frontendBasePath' => '/',
];
$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['crawler'] = $configuration;
$crawlStrategy = GeneralUtility::makeInstance(CrawlStrategyFactory::class)->create();

self::assertInstanceOf(
SubProcessExecutionStrategy::class,
$crawlStrategy
);
}
}
42 changes: 6 additions & 36 deletions Tests/Unit/QueueExecutorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,54 +17,24 @@
* The TYPO3 project - inspiring people to share!
*/

use AOE\Crawler\Configuration\ExtensionConfigurationProvider;
use AOE\Crawler\Controller\CrawlerController;
use AOE\Crawler\CrawlStrategy\GuzzleExecutionStrategy;
use AOE\Crawler\CrawlStrategy\SubProcessExecutionStrategy;
use AOE\Crawler\CrawlStrategy\CrawlStrategyFactory;
use AOE\Crawler\QueueExecutor;
use Nimut\TestingFramework\TestCase\UnitTestCase;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class QueueExecutorTest extends UnitTestCase
{
/**
* @test
*/
public function validateTypo3InternalGuzzleExecutionIsSelected(): void
{
$configuration = [
'makeDirectRequests' => 0,
'frontendBasePath' => '/',
];
$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['crawler'] = $configuration;
$subject = $this->getAccessibleMock(QueueExecutor::class, ['dummy']);
$res = $subject->_get('selectedStrategy');
self::assertEquals(get_class($res), GuzzleExecutionStrategy::class);
}

/**
* @test
*/
public function validateDirectExecutionIsSelected(): void
{
$configuration = [
'makeDirectRequests' => 1,
'frontendBasePath' => '/',
];
$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['crawler'] = $configuration;
$subject = $this->getAccessibleMock(QueueExecutor::class, ['dummy']);
$res = $subject->_get('selectedStrategy');
self::assertEquals(get_class($res), SubProcessExecutionStrategy::class);
}

/**
* @test
*/
public function invalidArgumentsReturnErrorInExecuteQueueItem(): void
{
$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['crawler'] = [];

$crawlerController = $this->createMock(CrawlerController::class);
$settings = $this->createMock(ExtensionConfigurationProvider::class);
$settings->expects($this->once())->method('getExtensionConfiguration')->willReturn([]);
$subject = new QueueExecutor($settings);
$crawlStrategyFactory = GeneralUtility::makeInstance(CrawlStrategyFactory::class);
$subject = new QueueExecutor($crawlStrategyFactory);
$result = $subject->executeQueueItem([], $crawlerController);
self::assertEquals('ERROR', $result);
}
Expand Down

0 comments on commit c9d0889

Please sign in to comment.