Skip to content

Commit a64f22d

Browse files
committed
When events requested without project, events for all projects should be returned
1 parent 46923e7 commit a64f22d

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

app/modules/Events/Interfaces/Queries/CountEventsHandler.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,20 @@
77
use App\Application\Commands\CountEvents;
88
use Modules\Events\Domain\EventRepositoryInterface;
99
use Spiral\Cqrs\Attribute\QueryHandler;
10+
use Spiral\Cqrs\QueryBusInterface;
1011

1112
final class CountEventsHandler extends EventsHandler
1213
{
1314
public function __construct(
1415
private readonly EventRepositoryInterface $events,
15-
) {}
16+
QueryBusInterface $bus,
17+
) {
18+
parent::__construct($bus);
19+
}
1620

1721
#[QueryHandler]
1822
public function __invoke(CountEvents $query): int
1923
{
20-
return $this->events->countAll(self::getScopeFromFindEvents($query));
24+
return $this->events->countAll($this->getScopeFromFindEvents($query));
2125
}
2226
}

app/modules/Events/Interfaces/Queries/EventsHandler.php

+21-2
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,36 @@
33
namespace Modules\Events\Interfaces\Queries;
44

55
use App\Application\Commands\AskEvents;
6+
use App\Application\Commands\FindAllProjects;
7+
use Modules\Projects\Domain\ProjectInterface;
8+
use Modules\Projects\Domain\ValueObject\Key;
9+
use Spiral\Cqrs\QueryBusInterface;
610

711
abstract class EventsHandler
812
{
9-
protected static function getScopeFromFindEvents(AskEvents $query): array
13+
public function __construct(
14+
private readonly QueryBusInterface $bus,
15+
) {}
16+
17+
protected function getScopeFromFindEvents(AskEvents $query): array
1018
{
1119
$scope = [];
1220
if ($query->type !== null) {
1321
$scope['type'] = $query->type;
1422
}
1523

16-
$scope['project'] = $query->project;
24+
if ($query->project !== null) {
25+
$scope['project'] = $query->project;
26+
} elseif ($query->project !== []) {
27+
// TODO: refactor this
28+
$projects = $this->bus->ask(new FindAllProjects());
29+
$keys = \array_map(
30+
static fn(ProjectInterface $project): Key => $project->getKey(),
31+
\iterator_to_array($projects),
32+
);
33+
34+
$scope['project'] = $keys;
35+
}
1736

1837
return $scope;
1938
}

app/modules/Events/Interfaces/Queries/FindEventsHandler.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,24 @@
66

77
use App\Application\Commands\FindEvents;
88
use Modules\Events\Domain\EventRepositoryInterface;
9+
use Spiral\Core\Container;
910
use Spiral\Cqrs\Attribute\QueryHandler;
11+
use Spiral\Cqrs\QueryBusInterface;
1012

1113
final class FindEventsHandler extends EventsHandler
1214
{
1315
public function __construct(
1416
private readonly EventRepositoryInterface $events,
15-
) {}
17+
QueryBusInterface $bus,
18+
) {
19+
parent::__construct($bus);
20+
}
1621

1722
#[QueryHandler]
1823
public function __invoke(FindEvents $query): iterable
1924
{
2025
return $this->events->findAll(
21-
scope: self::getScopeFromFindEvents($query),
26+
scope: $this->getScopeFromFindEvents($query),
2227
orderBy: ['timestamp' => 'desc'],
2328
limit: $query->limit,
2429
);

app/src/Application/Commands/FindAllProjects.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
namespace App\Application\Commands;
66

7-
use Modules\Projects\Domain\Project;
7+
use Modules\Projects\Domain\ProjectInterface;
88
use Spiral\Cqrs\QueryInterface;
99

1010
/**
11-
* @implements QueryInterface<Project[]>
11+
* @implements QueryInterface<ProjectInterface[]>
1212
*/
1313
final class FindAllProjects implements QueryInterface {}

0 commit comments

Comments
 (0)