Skip to content

Commit ae3e0e6

Browse files
authored
Merge pull request #231 from buggregator/feature/default-project
Improves information about default project
2 parents ab82861 + 7640bb1 commit ae3e0e6

File tree

6 files changed

+38
-13
lines changed

6 files changed

+38
-13
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

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

1112
final class FindEventsHandler 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(FindEvents $query): iterable
1923
{
2024
return $this->events->findAll(
21-
scope: self::getScopeFromFindEvents($query),
25+
scope: $this->getScopeFromFindEvents($query),
2226
orderBy: ['timestamp' => 'desc'],
2327
limit: $query->limit,
2428
);

app/modules/Projects/Interfaces/Http/Resources/ProjectResource.php

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Modules\Projects\Interfaces\Http\Resources;
66

77
use App\Application\HTTP\Response\JsonResource;
8+
use Modules\Projects\Domain\Project;
89
use Modules\Projects\Domain\ProjectInterface;
910
use OpenApi\Attributes as OA;
1011

@@ -16,6 +17,7 @@
1617
properties: [
1718
new OA\Property(property: 'key', type: 'string'),
1819
new OA\Property(property: 'name', type: 'string'),
20+
new OA\Property(property: 'is_default', type: 'boolean'),
1921
],
2022
)]
2123
final class ProjectResource extends JsonResource
@@ -30,6 +32,7 @@ protected function mapData(): array|\JsonSerializable
3032
return [
3133
'key' => $this->data->getKey(),
3234
'name' => $this->data->getName(),
35+
'is_default' => (string) $this->data->getKey() === Project::DEFAULT_KEY,
3336
];
3437
}
3538
}

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 {}

app/src/Interfaces/Http/Controller/SettingsAction.php

-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use App\Application\HTTP\Response\JsonResource;
1010
use App\Application\HTTP\Response\ResourceInterface;
1111
use App\Application\Ide\UrlTemplate;
12-
use Modules\Projects\Domain\Project;
1312
use Spiral\Boot\EnvironmentInterface;
1413
use Spiral\Router\Annotation\Route;
1514

@@ -31,10 +30,6 @@ public function __invoke(
3130
'url_template' => $ideUrl->template,
3231
],
3332
'version' => $appVersion->version,
34-
'project' => [
35-
// todo: use better option for default project
36-
'default' => Project::DEFAULT_KEY,
37-
],
3833
]);
3934
}
4035
}

0 commit comments

Comments
 (0)