HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

Uploading file for related entity

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
fileuploadingrelatedforentity

Problem

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:

  • new.html.twig



  • Book.php



  • BookType.php



  • BookCover.php



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.

Solution

BookController.php

I don't see anything to change, the only thing which i could do different is the return line from

return $this->render('MatiitBookBundle:Book:new.html.twig', ['form' => $form->createView()]);


i would write something like this to make it more clear

return $this->render(
    'MatiitBookBundle:Book:new.html.twig', 
    [
        'form' => $form -> createView()
    ]
);


So if one day you need to add another line you will just add a , and the code will still look good.

new.html.twig

Why var $file? Why this $? It remember me something which is related to jQuery... Do you really need this? I would just do var file or choose a more specific name. (no idea right now, file could be ok but what is book_file? a input type="file"?)

Book.php

You use documentation only to specify the type of the fields? You could start with add the documentation of the fields and document all methods (or at least public/protected methods)

I'm wrong or here the indentation is a bit messed up? Could be just a copy/paste bug, anyway from protected $category; it wil look like you start write an inner class. (same for the end of the file)

Fix it if it's in your code.

What is length? I'm not English so maybe i'm wrong but i don't think length is a great name for a Book class (without documentation it's more hard to understand what is it).

It's the book pages? What about totalPages? It's the length (size) of the file in bytes? What about fileSize?

BookType.php

class BookType extends AbstractType


Why not call your class AbstractType as AbstractBookType?

AbstractType could sound as something which is related to variables type, not your class.

I know you use namespaces but i think it's important to make the name of something very clear.

Here you could add documentation too.

BookCover.php

It looks so similar to Book.php

Some methods are similar, maybe Book should have an instance of BookCover inside instead of recreate the methods from zero?

I need more info about it.

protected function getUploadRootDir()
{
   // the absolute directory path where uploaded
   // documents should be saved
   return __DIR__.'/../../../../../web/'.$this->getUploadDir();
}


I'm not a PHP expert, but what if one day you change the location? You should remember to edit this method inside BookCover and inside Book too! Can't you make it in a static field for the two classes?

And you should maintain the /../../../../../ to point in the new location!

Anyway my review is more about style than other.

Code Snippets

return $this->render('MatiitBookBundle:Book:new.html.twig', ['form' => $form->createView()]);
return $this->render(
    'MatiitBookBundle:Book:new.html.twig', 
    [
        'form' => $form -> createView()
    ]
);
class BookType extends AbstractType
protected function getUploadRootDir()
{
   // the absolute directory path where uploaded
   // documents should be saved
   return __DIR__.'/../../../../../web/'.$this->getUploadDir();
}

Context

StackExchange Code Review Q#51161, answer score: 2

Revisions (0)

No revisions yet.