I see that my code smells. The first file is a controller, and createAction. But I don't know where to delegate this.
BookController.php - Whole controller class
So this is most 'smelly' code to me:
public function createAction(Request $request) {
$user = $this->get('security.context')->getToken()->getUser();
$book = new Book();
$form = $this->createForm(new BookType($user->getId()), $book);
$form->handleRequest($request);
if($form->isValid()) {
$book->upload();
$em = $this->getDoctrine()->getManager();
$em->persist($book);
$em->flush();
$covers = $request->files->get('covers');
foreach ($covers as $cover) {
$coverObj = new BookCover();
$coverObj->setFile($cover);
$coverObj->setBook($book);
$coverObj->upload();
$em->persist($coverObj);
}
$em->flush();
return $this->redirect($this->generateUrl('matiit_book_homepage'));
}
return $this->render('MatiitBookBundle:Book:new.html.twig', ['form' => $form->createView()]);
}
Rest of files:
Uploading in entities is the second thing which bothers me.
It would be nice to know what is worth testing here - I have no experience with tests so it would be very useful for me.
postPersist
for the upload in your book, also why are you using$coverObj->upload();
when it should already be covered by thepostPersist
? Also I don't think the first$em->flush()
is needed. How come you have a fieldfile
in your form but are getting the files from an uploadcovers
? \$\endgroup\$