src/App/Controller/ContentReport/VerifyController.php line 40

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace App\Controller\ContentReport;
  3. use App\Controller\CRUD\EditController;
  4. use App\Entity\ContentReport;
  5. use App\Form\Type\EmptyType;
  6. use Sindrive\EmailSenderBundle\Service\EmailSender;
  7. use Symfony\Component\Form\FormInterface;
  8. use Symfony\Component\HttpFoundation\ParameterBag;
  9. use Symfony\Component\HttpFoundation\Request;
  10. /**
  11.  * @extends EditController<ContentReport>
  12.  */
  13. class VerifyController extends EditController
  14. {
  15.     private EmailSender $emailSender;
  16.     public function __construct(EmailSender $emailSender)
  17.     {
  18.         $this->emailSender $emailSender;
  19.         parent::__construct(
  20.             ContentReport::class,
  21.             EmptyType::class,
  22.             'none',
  23.             'content_report'
  24.         );
  25.     }
  26.     public function provideEntity(ParameterBag $parameters): object
  27.     {
  28.         if (null === $hash $parameters->get('hash')) {
  29.             throw $this->createNotFoundException('Hash is not set.');
  30.         }
  31.         if (null === $entity $this->getRepository()->findOneBy(['hash' => $hash'verified' => false])) {
  32.             throw $this->createNotFoundException('No content report found.');
  33.         }
  34.         return $entity;
  35.     }
  36.     /**
  37.      * @phpstan-param ContentReport $entity
  38.      * @return array<mixed>
  39.      */
  40.     protected function getRedirectUrlParameters(Request $requestFormInterface $form$entity): array
  41.     {
  42.         return [];
  43.     }
  44.     /**
  45.      * @phpstan-param ContentReport $entity
  46.      */
  47.     protected function isValid(Request $requestFormInterface $form, &$entity): bool
  48.     {
  49.         if ($entity->getHash() !== $request->attributes->get('hash')) {
  50.             throw $this->createNotFoundException('Wrong hash provided.');
  51.         }
  52.         return true;
  53.     }
  54.     /**
  55.      * @phpstan-param ContentReport $entity
  56.      */
  57.     protected function beforeUpdate(Request $requestFormInterface $form$entity): void
  58.     {
  59.         $entity->setVerified(true);
  60.     }
  61.     /**
  62.      * @phpstan-param ContentReport $entity
  63.      */
  64.     protected function afterUpdate(Request $requestFormInterface $form$entity): void
  65.     {
  66.         $this->emailSender->sendType(new \App\Email\ContentReport($entity));
  67.     }
  68.     /**
  69.      * @phpstan-param ContentReport $entity
  70.      */
  71.     protected function showSavedMessage($entity): void
  72.     {
  73.         $this->formSavedMessage->setEnabled(true);
  74.         $this->formSavedMessage->showMessage($entity$this->translator->trans('verification.success_message', [], 'content_report'));
  75.     }
  76. }