Skip to content

Commit 9b51eaf

Browse files
committed
[TASK] refactor signal slot to event
1 parent 6736d42 commit 9b51eaf

File tree

3 files changed

+60
-12
lines changed

3 files changed

+60
-12
lines changed

Classes/Domain/Repository/JsonSearchRepository.php

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace Eike\Yacy\Domain\Repository;
33

4+
use Eike\Yacy\Event\BeforeReturnResultsEvent;
5+
use Psr\EventDispatcher\EventDispatcherInterface;
46
use TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotException;
57
use TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotReturnException;
68
use TYPO3\CMS\Core\Log\LogManager;
@@ -40,6 +42,10 @@
4042
*/
4143
class JsonSearchRepository extends AbstractSearchRepository
4244
{
45+
public function __construct(
46+
private readonly EventDispatcherInterface $eventDispatcher,
47+
) {}
48+
4349
/**
4450
* @param Demand $demand
4551
* @param int $page
@@ -51,22 +57,20 @@ public function findDemanded(Demand $demand, $page = 1, $debug = 0)
5157
{
5258

5359
try{
54-
$json = json_decode(file_get_contents($demand->getRequestUrl()), true);
60+
/** @var BeforeReturnResultsEvent $event */
61+
$event = $this->eventDispatcher->dispatch(
62+
new BeforeReturnResultsEvent(
63+
$demand,
64+
$page,
65+
json_decode(file_get_contents($demand->getRequestUrl()), true)
66+
),
67+
);
68+
return $event->getJson()['channels'][0];
5569
}catch(\Exception $exception){
5670
GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__)->log(LogLevel::INFO, $exception->getMessage(), '');
5771
if($debug === '1'){
5872
throw $exception;
5973
}
6074
}
61-
62-
/*todo this needs to be refactored to events
63-
* https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/10.4/Deprecation-90625-ExtbaseSignalSlotDispatcher.html
64-
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
65-
66-
$signalSlotDispatcher = $objectManager->get(Dispatcher::class);
67-
$signalSlotDispatcher->dispatch(__CLASS__, 'beforeReturnResults', [$demand, $page, &$json]);
68-
*/
69-
return $json['channels'][0];
70-
7175
}
7276
}
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Eike\Yacy\Event;
5+
6+
use Eike\Yacy\Domain\Model\Demand;
7+
8+
final class BeforeReturnResultsEvent
9+
{
10+
public function __construct(
11+
private Demand $demand,
12+
private readonly int $page,
13+
private mixed $json
14+
) {}
15+
16+
public function getDemand(): Demand
17+
{
18+
return $this->demand;
19+
}
20+
21+
public function setDemand(Demand $demand): BeforeReturnResultsEvent
22+
{
23+
$this->demand = $demand;
24+
return $this;
25+
}
26+
27+
public function getJson(): mixed
28+
{
29+
return $this->json;
30+
}
31+
32+
public function setJson(mixed $json): BeforeReturnResultsEvent
33+
{
34+
$this->json = $json;
35+
return $this;
36+
}
37+
38+
public function getPage(): int
39+
{
40+
return $this->page;
41+
}
42+
}

Classes/Factory/SearchRepositoryFactory.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
use Eike\Yacy\Domain\Repository\JsonSearchRepository;
3030
use Eike\Yacy\Domain\Repository\RssSearchRepository;
31+
use Psr\EventDispatcher\EventDispatcherInterface;
3132
use TYPO3\CMS\Core\Utility\GeneralUtility;
3233
use TYPO3\CMS\Extbase\Object\ObjectManager;
3334

@@ -43,7 +44,8 @@ public function createSearchRepository($interfaceName)
4344
return GeneralUtility::makeInstance(RssSearchRepository::class);
4445
}
4546
if ($interfaceName === 'yacysearch.json') {
46-
return GeneralUtility::makeInstance(JsonSearchRepository::class);
47+
$eventDispatcher = GeneralUtility::makeInstance(EventDispatcherInterface::class);
48+
return GeneralUtility::makeInstance(JsonSearchRepository::class,$eventDispatcher);
4749
}
4850
return null;
4951
}

0 commit comments

Comments
 (0)