Skip to content

Commit 73f2fae

Browse files
Add recent searches
1 parent 8e2956a commit 73f2fae

File tree

8 files changed

+64
-6
lines changed

8 files changed

+64
-6
lines changed

public/index.php

+9
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66

77
use Monolog\Logger;
88
use Reconmap\ApiRouter;
9+
use Reconmap\Events\SearchEvent;
910
use Reconmap\Services\ApplicationConfig;
1011
use Reconmap\Services\ApplicationContainer;
1112
use Reconmap\Services\Logging\LoggingConfigurator;
13+
use Reconmap\Services\SearchListener;
1214
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
1315
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
16+
use Symfony\Component\EventDispatcher\EventDispatcher;
1417
use Symfony\Component\HttpFoundation\Response;
1518

1619
$configFilePath = $applicationDir . '/config.json';
@@ -47,6 +50,12 @@
4750
$router = new ApiRouter();
4851
$router->mapRoutes($container, $config);
4952

53+
/**
54+
* @var EventDispatcher $eventDispatcher
55+
*/
56+
$eventDispatcher = $container->get(EventDispatcher::class);
57+
$eventDispatcher->addListener(SearchEvent::class, $container->get(SearchListener::class));
58+
5059
$response = $router->dispatch($request);
5160

5261
$httpFoundationFactory = new HttpFoundationFactory();

src/Controllers/Projects/GetProjectsController.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@
66
use Psr\Http\Message\ResponseInterface;
77
use Psr\Http\Message\ServerRequestInterface;
88
use Reconmap\Controllers\Controller;
9+
use Reconmap\Events\SearchEvent;
910
use Reconmap\Repositories\ProjectRepository;
1011
use Reconmap\Repositories\SearchCriterias\ProjectSearchCriteria;
1112
use Reconmap\Services\PaginationRequestHandler;
13+
use Symfony\Component\EventDispatcher\EventDispatcher;
1214

1315
class GetProjectsController extends Controller
1416
{
1517
public function __construct(private readonly ProjectRepository $projectRepository,
16-
private readonly ProjectSearchCriteria $projectSearchCriteria)
18+
private readonly ProjectSearchCriteria $projectSearchCriteria,
19+
private readonly EventDispatcher $eventDispatcher)
1720
{
1821
}
1922

@@ -25,6 +28,7 @@ public function __invoke(ServerRequestInterface $request): ResponseInterface
2528

2629
if (isset($params['keywords'])) {
2730
$this->projectSearchCriteria->addKeywordsCriterion($params['keywords']);
31+
$this->eventDispatcher->dispatch(new SearchEvent($user->id, $params['keywords']));
2832
}
2933
if (isset($params['clientId'])) {
3034
$this->projectSearchCriteria->addClientCriterion(intval($params['clientId']));

src/Controllers/System/GetRecentSearchesController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ protected function process(ApplicationRequest $request): ResponseInterface
1919
$userId = $request->getUser()->id;
2020
$setName = "recent-searches-user{$userId}";
2121

22-
$recentSearches = $this->redisServer->zRange($setName, 0, 10);
22+
$recentSearches = $this->redisServer->zRevRange($setName, 0, 9);
2323

2424
$response = $this->createOkResponse();
2525
$response->getBody()->write(json_encode($recentSearches));

src/Events/SearchEvent.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Reconmap\Events;
4+
5+
use Symfony\Contracts\EventDispatcher\Event;
6+
7+
class SearchEvent extends Event
8+
{
9+
public function __construct(private readonly int $userId, private readonly string $keywords)
10+
{
11+
}
12+
13+
public function getUserId(): int
14+
{
15+
return $this->userId;
16+
}
17+
18+
public function getKeywords(): string
19+
{
20+
return $this->keywords;
21+
}
22+
}

src/Services/ApplicationContainer.php

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
1313
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
1414
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
15+
use Symfony\Component\EventDispatcher\EventDispatcher;
1516
use Symfony\Component\Filesystem\Filesystem;
1617

1718
class ApplicationContainer extends ContainerBuilder
@@ -37,6 +38,7 @@ public static function initialise(ContainerInterface $container, ApplicationConf
3738
$container->set(Logger::class, $logger);
3839
$container->set(ProcessorFactory::class, new ProcessorFactory());
3940
$container->set(ContainerInterface::class, $container);
41+
$container->set(EventDispatcher::class, new EventDispatcher());
4042
}
4143

4244
private function configure(ContainerConfigurator $containerConfigurator): void
@@ -62,7 +64,9 @@ private function configure(ContainerConfigurator $containerConfigurator): void
6264
->set(ApplicationConfig::class)->synthetic()
6365
->set(ProcessorFactory::class)->synthetic()
6466
->set(ContainerInterface::class)->synthetic()
67+
->set(EventDispatcher::class)->synthetic()
6568
->set(\mysqli::class)->synthetic();
69+
6670
}
6771
}
6872

src/Services/AuditLogService.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Reconmap\Repositories\AuditLogRepository;
66

7-
class AuditLogService
7+
readonly class AuditLogService
88
{
99
public function __construct(
1010
private AuditLogRepository $repository,

src/Services/SearchListener.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Reconmap\Services;
4+
5+
use Reconmap\Events\SearchEvent;
6+
7+
readonly class SearchListener
8+
{
9+
public function __construct(private RedisServer $redisServer)
10+
{
11+
12+
}
13+
14+
public function __invoke(SearchEvent $searchEvent): void
15+
{
16+
$setName = "recent-searches-user{$searchEvent->getUserId()}";
17+
$this->redisServer->zAdd($setName, time(), $searchEvent->getKeywords());
18+
}
19+
}

src/Tasks/EmailTaskProcessor.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
use Symfony\Component\Mime\Address;
1111
use Symfony\Component\Mime\Email;
1212

13-
class EmailTaskProcessor implements ItemProcessor
13+
readonly class EmailTaskProcessor implements ItemProcessor
1414
{
15-
public function __construct(private readonly ApplicationConfig $config,
16-
private readonly Logger $logger)
15+
public function __construct(private ApplicationConfig $config,
16+
private Logger $logger)
1717
{
1818
}
1919

0 commit comments

Comments
 (0)