Completed
Push — master ( 6d6774...64f3ed )
by Jeroen
11:23 queued 05:13
created

MediaBundle/Controller/AviaryController.php (2 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\MediaBundle\Controller;
4
5
use Kunstmaan\MediaBundle\Entity\Folder;
6
use Kunstmaan\MediaBundle\Entity\Media;
7
use Kunstmaan\MediaBundle\Helper\MediaManager;
8
use Symfony\Component\Routing\Annotation\Route;
9
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
10
use Symfony\Component\HttpFoundation\RedirectResponse;
11
use Symfony\Component\HttpFoundation\Request;
12
13
/**
14
 * Controller class which Aviary can use to upload the edited image and add it to the database
15
 */
16
class AviaryController extends Controller
17
{
18
    /**
19
     * @param Request $request
20
     * @param int     $folderId The id of the Folder
21
     * @param int     $mediaId  The id of the image
22
     *
23
     * @Route("/aviary/{folderId}/{mediaId}", requirements={"folderId" = "\d+", "mediaId" = "\d+"}, name="KunstmaanMediaBundle_aviary")
24
     *
25
     * @return RedirectResponse
26
     */
27
    public function indexAction(Request $request, $folderId, $mediaId)
28
    {
29
        $em = $this->getDoctrine()->getManager();
30
31
        /* @var Folder $folder */
32
        $folder = $em->getRepository('KunstmaanMediaBundle:Folder')->getFolder($folderId);
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Doctrine\Persistence\ObjectRepository as the method getFolder() does only exist in the following implementations of said interface: Kunstmaan\MediaBundle\Repository\FolderRepository.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
33
        /* @var Media $media */
34
        $media = $em->getRepository('KunstmaanMediaBundle:Media')->getMedia($mediaId);
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Doctrine\Persistence\ObjectRepository as the method getMedia() does only exist in the following implementations of said interface: Kunstmaan\MediaBundle\Repository\MediaRepository.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
35
        /* @var MediaManager $mediaManager */
36
        $mediaManager = $this->get('kunstmaan_media.media_manager');
37
38
        $media = clone $media;
39
        $handler = $mediaManager->getHandler($media);
40
        $fileHelper = $handler->getFormHelper($media);
41
        $fileHelper->getMediaFromUrl($request->get('url'));
42
        $media = $fileHelper->getMedia();
43
44
        $media->setUuid(null);
45
        $handler->prepareMedia($media);
46
47
        $em->persist($media);
48
        $em->flush();
49
50
        $media->setCreatedAt($media->getUpdatedAt());
51
        $em->persist($media);
52
        $em->flush();
53
54
        return new RedirectResponse(
55
            $this->generateUrl(
56
                'KunstmaanMediaBundle_folder_show',
57
                array('folderId' => $folder->getId())
58
            )
59
        );
60
    }
61
}
62