Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds projects support #150

Merged
merged 21 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Adds CQRS command to create project
  • Loading branch information
butschster committed Apr 27, 2024
commit 6140f6f78f884fa3ab09efc4b7495e7e1ca61762
39 changes: 39 additions & 0 deletions app/modules/Projects/Interfaces/Commands/CreateProjectHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Modules\Projects\Interfaces\Commands;

use App\Application\Commands\CreateProject;
use Modules\Projects\Domain\Project;
use Modules\Projects\Domain\ProjectFactoryInterface;
use Modules\Projects\Domain\ProjectRepositoryInterface;
use Modules\Projects\Domain\ValueObject\Key;
use Modules\Projects\Exception\UniqueKeyException;
use Spiral\Cqrs\Attribute\CommandHandler;

final readonly class CreateProjectHandler
{
public function __construct(
private ProjectFactoryInterface $factory,
private ProjectRepositoryInterface $projects,
) {
}

#[CommandHandler]
public function __invoke(CreateProject $command): Project
{
if ($this->projects->findByPK($command->key) !== null) {
throw new UniqueKeyException("Project with key {$command->key} already exists");
}

$project = $this->factory->create(
key: Key::create($command->key),
name: $command->name,
);

$this->projects->store($project);

return $project;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

namespace Modules\Projects\Interfaces\Console\Command;

use App\Application\Commands\CreateProject;
use Modules\Projects\Domain\ProjectLocatorInterface;
use Modules\Projects\Domain\ProjectRepositoryInterface;
use Spiral\Console\Attribute\AsCommand;
use Spiral\Console\Command;
use Spiral\Cqrs\CommandBusInterface;

#[AsCommand(
name: 'projects:register',
Expand All @@ -16,17 +17,21 @@
final class RegisterCommand extends Command
{
public function __invoke(
ProjectRepositoryInterface $projects,
CommandBusInterface $bus,
ProjectLocatorInterface $locator,
): int {
foreach ($locator->findAll() as $project) {
if ($projects->findByPK($project->getKey()) !== null) {
$this->writeln("Project already registered: {$project->getName()}");
continue;
try {
$this->writeln("Registering project: {$project->getName()}");
$bus->dispatch(
new CreateProject(
key: (string)$project->getKey(),
name: $project->getName(),
),
);
} catch (\Throwable $e) {
$this->error($e->getMessage());
}

$this->writeln("Registering project: {$project->getName()}");
$projects->store($project);
}

return self::SUCCESS;
Expand Down
16 changes: 16 additions & 0 deletions app/src/Application/Commands/CreateProject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace App\Application\Commands;

use Spiral\Cqrs\CommandInterface;

final readonly class CreateProject implements CommandInterface
{
public function __construct(
public string $key,
public string $name,
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ public function __construct(
public function store(Project $project): bool
{
$key = $project->getKey();

$projects = $this->getProjects();
$exists = $this->findByPK($key);
if ($exists) {
throw new UniqueKeyException(\sprintf('Project with key %s already exists', $key));
}

$projects = $this->getProjects();

$projects[] = [
'key' => (string)$project->getKey(),
'name' => $project->getName(),
Expand Down
6 changes: 0 additions & 6 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,4 @@
<directory suffix="Test.php">tests/Unit</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">app/src</directory>
<directory suffix=".php">app/modules</directory>
</include>
</coverage>
</phpunit>
Loading