WIP: feature_paste #1
|
@ -6,7 +6,6 @@ namespace App\Controller;
|
||||||
use App\Form\Type\PasteForm;
|
use App\Form\Type\PasteForm;
|
||||||
use App\Entity\Paste;
|
use App\Entity\Paste;
|
||||||
use App\Repository\PasteRepository;
|
use App\Repository\PasteRepository;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
use Symfony\Component\Routing\Annotation\Route;
|
use Symfony\Component\Routing\Annotation\Route;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
@ -16,7 +15,7 @@ use Symfony\Component\HttpFoundation\Request;
|
||||||
class PasteController extends AbstractController
|
class PasteController extends AbstractController
|
||||||
{
|
{
|
||||||
#[Route('/')]
|
#[Route('/')]
|
||||||
|
|||||||
public function new(Request $request, EntityManagerInterface $entityManager): Response {
|
public function new(Request $request, PasteRepository $pasteRepository): Response {
|
||||||
skobkin marked this conversation as resolved
Outdated
skobkin
commented
You don't really need Make your repository implement You don't really need `EntityManager` here.
Make your repository implement `save` like I showed you and use it instead.
|
|||||||
$paste = new Paste();
|
$paste = new Paste();
|
||||||
skobkin marked this conversation as resolved
Outdated
skobkin
commented
I'd suggest we refactor this to to DTO usage and make our I'd suggest we refactor this to to DTO usage and make our `Paste` entity immutable.
|
|||||||
$form = $this->createForm(PasteForm::class, $paste);
|
$form = $this->createForm(PasteForm::class, $paste);
|
||||||
|
|
||||||
|
@ -30,8 +29,7 @@ class PasteController extends AbstractController
|
||||||
$paste->setSecret(hash('sha1', random_bytes(25)));
|
$paste->setSecret(hash('sha1', random_bytes(25)));
|
||||||
skobkin marked this conversation as resolved
Outdated
skobkin
commented
Do we need double quotes here? By the way, we can also do that in the constructor. Do we need double quotes here?
By the way, we can also do that in the constructor.
skobkin marked this conversation as resolved
Outdated
skobkin
commented
This still could be done in the entity itself. This still could be done in the entity itself.
|
|||||||
}
|
}
|
||||||
|
|
||||||
$entityManager->persist($paste);
|
$pasteRepository->save($paste);
|
||||||
$entityManager->flush();
|
|
||||||
|
|
||||||
skobkin marked this conversation as resolved
Outdated
skobkin
commented
I still suggest to make I still suggest to make `Paste::fromFormData()`.
|
|||||||
return $this->redirectToRoute($request->attributes->get('_route'));
|
return $this->redirectToRoute($request->attributes->get('_route'));
|
||||||
|
|
||||||
skobkin marked this conversation as resolved
Outdated
skobkin
commented
Just set a BTW, where are you redirecting exactly? 🤔 You should be redirecting to the route which is processed by Just set a `name` for your route and you won't need such workarounds.
BTW, where are you redirecting exactly? 🤔 You should be redirecting to the route which is processed by `PasteController::show_paste()` as far as I understand your code.
|
|||||||
|
|
|
@ -12,4 +12,10 @@ class PasteRepository extends ServiceEntityRepository
|
||||||
public function __construct(ManagerRegistry $registry) {
|
public function __construct(ManagerRegistry $registry) {
|
||||||
parent::__construct($registry, Paste::class);
|
parent::__construct($registry, Paste::class);
|
||||||
skobkin marked this conversation as resolved
Outdated
skobkin
commented
I'd suggest adding I'd suggest adding `save()` here like I showed you.
skobkin
commented
Better with optional Better with optional `flush`.
Flushng from the repo itself should be a rare action.
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
public function save(Paste $paste): void {
|
||||||
|
$entityManager = $this->getEntityManager();
|
||||||
skobkin marked this conversation as resolved
Outdated
skobkin
commented
Do not forget to use code formatting. It'd surrounded Do not forget to use code formatting. It'd surrounded `=` with spaces for you.
|
|||||||
|
$entityManager->persist($paste);
|
||||||
|
$entityManager->flush();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
skobkin marked this conversation as resolved
Outdated
skobkin
commented
You shouldn't do flush here by default. Only in certain cases if needed. You shouldn't do flush here by default. Only in certain cases if needed.
|
I'd suggest explicitly defining which request methods we're processing here.
P.S. We'll move this to YAML in the end.