Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 20 additions & 0 deletions src/Application/Command/Report/CreateReportCommentCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace App\Application\Command\Report;

use Symfony\Component\Validator\Constraints as Assert;

readonly class CreateReportCommentCommand
{
public function __construct(
#[Assert\NotBlank]
public string $comment,
#[Assert\NotBlank, Assert\Ip]
public string $ip,
#[Assert\NotBlank]
public string $reportId,
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace App\Application\CommandHandler\Report;

use App\Application\Command\Report\CreateReportCommentCommand;
use App\Application\Exception\Report\ReportNotFoundException;
use App\Application\Helper\MessageBusHelper;
use App\Application\Query\Report\GetReportByIdQuery;
use App\Domain\Factory\Report\ReportCommentFactory;
use App\Domain\Model\Report\Report;
use App\Domain\Repository\Report\ReportCommentRepositoryInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Messenger\MessageBusInterface;

class CreateReportCommentCommandHandler
{
public function __construct(
private readonly ReportCommentRepositoryInterface $reportCommentRepository,
private readonly MessageBusInterface $messageBus,
private readonly MessageBusHelper $messageBusHelper,
private readonly LoggerInterface $logger,
) {
}

public function __invoke(CreateReportCommentCommand $command): void
{
$reportEnvelope = $this->messageBus->dispatch(new GetReportByIdQuery($command->reportId));
/** @var Report $report */
$report = $this->messageBusHelper->getContentFromEnvelope(
envelope: $reportEnvelope,
logErrorMessage: 'Report not found',
class: Report::class,
);

if (null === $report) {
$this->logger->error(
sprintf('Report %s not found', $command->reportId),
);
throw new ReportNotFoundException();
}

$reportComment = ReportCommentFactory::create(
comment: $command->comment,
ip: $command->ip,
report: $report,
);

$this->reportCommentRepository->save($reportComment);
}
}
9 changes: 9 additions & 0 deletions src/Application/Exception/Report/ReportNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace App\Application\Exception\Report;

class ReportNotFoundException extends \InvalidArgumentException
{
}
16 changes: 16 additions & 0 deletions src/Application/Query/Report/GetReportByIdQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace App\Application\Query\Report;

use Symfony\Component\Validator\Constraints as Assert;

readonly class GetReportByIdQuery
{
public function __construct(
#[Assert\NotBlank]
public string $reportId,
) {
}
}
23 changes: 23 additions & 0 deletions src/Application/QueryHandler/Report/GetReportByIdQueryHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace App\Application\QueryHandler\Report;

use App\Application\Query\Report\GetReportByIdQuery;
use App\Domain\Model\Report\Report;
use App\Domain\Repository\Report\ReportRepositoryInterface;

class GetReportByIdQueryHandler
{
public function __construct(
private readonly ReportRepositoryInterface $reportRepository,
) {
}

public function __invoke(GetReportByIdQuery $query): ?Report
{
/** @var ?Report */
return $this->reportRepository->find($query->reportId);
}
}
23 changes: 23 additions & 0 deletions src/Domain/Factory/Report/ReportCommentFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace App\Domain\Factory\Report;

use App\Domain\Model\Report\Report;
use App\Domain\Model\Report\ReportComment;

class ReportCommentFactory
{
public static function create(
string $comment,
string $ip,
Report $report,
): ReportComment {
return new ReportComment(
comment: $comment,
ip: $ip,
report: $report,
);
}
}
46 changes: 46 additions & 0 deletions src/Presentation/Controller/CreateReportCommentController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace App\Presentation\Controller;

use App\Application\Command\Report\CreateReportCommentCommand;
use App\Presentation\Dto\CreateReportCommentFormInputDto;
use App\Presentation\Form\CreateReportCommentForm;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Routing\Attribute\Route;

class CreateReportCommentController extends AbstractController
{
#[Route(path: '/reports/{reportId}/comment/new', name: 'create_report_comment', methods: [Request::METHOD_POST])]
public function __invoke(
Request $request,
string $reportId,
MessageBusInterface $messageBus,
): Response {
$createReportCommentForm = $this->createForm(CreateReportCommentForm::class);
$createReportCommentForm->handleRequest($request);

/** @var string $formDataGameSlug */
$formDataGameSlug = $request->request->all()['create_report_comment_form']['gameSlug'];

if (
$createReportCommentForm->isSubmitted()
&& 0 === $createReportCommentForm->getErrors()->count()
) {
/** @var CreateReportCommentFormInputDto $createReportCommentFormInputDto */
$createReportCommentFormInputDto = $createReportCommentForm->getData();

$messageBus->dispatch(new CreateReportCommentCommand(
$createReportCommentFormInputDto->comment,
$createReportCommentFormInputDto->ip,
$reportId,
));
}

return $this->redirectToRoute('reports_for_game', ['gameSlug' => $formDataGameSlug]);
}
}
7 changes: 6 additions & 1 deletion src/Presentation/Controller/ReportCreateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\Exception\ExceptionInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Routing\Attribute\Route;

Expand All @@ -32,7 +33,11 @@ public function __invoke(
/** @var CreateReportFormInputDto $createReportInputDto */
$createReportInputDto = $createReportForm->getData();

$messageBus->dispatch(new CreateReportFromDtoCommand($createReportInputDto));
try {
$messageBus->dispatch(new CreateReportFromDtoCommand($createReportInputDto));
} catch (ExceptionInterface) {
return $this->json('Error while creating a comment', Response::HTTP_UNPROCESSABLE_ENTITY);
}
}

return $this->redirectToRoute('reports_for_game', ['gameSlug' => $dataGameSlug]);
Expand Down
20 changes: 20 additions & 0 deletions src/Presentation/Dto/CreateReportCommentFormInputDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace App\Presentation\Dto;

use Symfony\Component\Validator\Constraints as Assert;

class CreateReportCommentFormInputDto
{
public function __construct(
#[Assert\NotBlank]
public string $comment = '',
#[Assert\NotBlank, Assert\Ip]
public string $ip = '',
#[Assert\NotBlank]
public string $gameSlug = '',
) {
}
}
47 changes: 47 additions & 0 deletions src/Presentation/Form/CreateReportCommentForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace App\Presentation\Form;

use App\Presentation\Dto\CreateReportCommentFormInputDto;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Contracts\Translation\TranslatorInterface;

class CreateReportCommentForm extends AbstractType
{
public function __construct(
private readonly TranslatorInterface $translator,
) {
}

public function buildForm(
FormBuilderInterface $builder,
array $options,
): void {
$builder
->add('comment', TextareaType::class, [
'label' => $this->translator->trans('form.create.comment.label', [], 'reportComment'),
'empty_data' => '',
'required' => true,
])
->add('ip', HiddenType::class, [
'required' => true,
])
->add('gameSlug', HiddenType::class, [
'required' => true,
])
;
}

public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => CreateReportCommentFormInputDto::class,
]);
}
}
Loading