Skip to content

Commit

Permalink
feature: switch to event listener internally (#1108)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasnorre authored Oct 8, 2024
1 parent ca9585b commit 9aafd39
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 18 deletions.
21 changes: 3 additions & 18 deletions Classes/Controller/CrawlerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -599,18 +599,9 @@ public function readUrl($queueId, $force = false, string $processId = '')
'result_data' => json_encode($result),
];

/** @var AfterQueueItemAddedEvent $event */
$event = $this->eventDispatcher->dispatch(new AfterQueueItemAddedEvent($queueId, $field_array));
$field_array = $event->getFieldArray();

//This should be extracted, we should listen to the event ourself,
// and move the code block here to an appropiate place. /Tomas 2024-10-07
GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable(QueueRepository::TABLE_NAME)
->update(QueueRepository::TABLE_NAME, $field_array, [
'qid' => (int) $queueId,
]);

$this->eventDispatcher->dispatch(new AfterQueueItemAddedEvent($queueId, $field_array));
$this->logger?->debug('crawler-readurl stop ' . microtime(true));

return $ret;
}

Expand All @@ -637,13 +628,7 @@ public function readUrlFromArray($field_array)
'result_data' => json_encode($result),
];

/** @var AfterQueueItemAddedEvent $event */
$event = $this->eventDispatcher->dispatch(new AfterQueueItemAddedEvent($queueId, $field_array));
$field_array = $event->getFieldArray();

$connectionForCrawlerQueue->update(QueueRepository::TABLE_NAME, $field_array, [
'qid' => $queueId,
]);
$this->eventDispatcher->dispatch(new AfterQueueItemAddedEvent($queueId, $field_array));

return $result;
}
Expand Down
21 changes: 21 additions & 0 deletions Classes/EventListener/AfterQueueItemAddedEventListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace AOE\Crawler\EventListener;

use AOE\Crawler\Domain\Repository\QueueRepository;
use AOE\Crawler\Event\AfterQueueItemAddedEvent;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class AfterQueueItemAddedEventListener
{
public function __invoke(AfterQueueItemAddedEvent $event): void
{
GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable(QueueRepository::TABLE_NAME)
->update(QueueRepository::TABLE_NAME, $event->getFieldArray(), [
'qid' => (int) $event->getQueueId(),
]);
}
}
5 changes: 5 additions & 0 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,8 @@ services:
AOE\Crawler\ContextMenu\ItemProvider:
tags:
- name: backend.contextmenu.itemprovider

AOE\Crawler\EventListener\AfterQueueItemAddedEventListener:
tags:
- name: event.listener
identifier: 'tx-crawler-after-queue-item-added'
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace AOE\Crawler\Tests\Functional\EventListener;

use AOE\Crawler\Domain\Repository\QueueRepository;
use AOE\Crawler\Event\AfterQueueItemAddedEvent;
use PHPUnit\Framework\Attributes\Test;
use TYPO3\CMS\Core\EventDispatcher\EventDispatcher;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;

class AfterQueueItemAddedEventListenerTest extends FunctionalTestCase
{
/**
* @var non-empty-string[]
*/
protected array $testExtensionsToLoad = ['typo3conf/ext/crawler'];

private EventDispatcher $eventDispatcher;
private QueueRepository $queueRepository;

protected function setUp(): void
{
parent::setUp();
$this->eventDispatcher = GeneralUtility::makeInstance(EventDispatcher::class);
$this->queueRepository = GeneralUtility::makeInstance(QueueRepository::class);
$this->importCSVDataSet(__DIR__ . '/../Fixtures/tx_crawler_queue.csv');
}

#[Test]
public function listenerIsInvoked(): void
{
$event = new AfterQueueItemAddedEvent(1001, [
'parameters' => 'test params',
]);
$this->eventDispatcher->dispatch($event);

$queueItem = $this->queueRepository->getQueueEntriesByQid(1001, true);
self::assertEquals('test params', $queueItem['parameters']);
}
}

0 comments on commit 9aafd39

Please sign in to comment.