Completed
Push — master ( ae5e03...0447ee )
by Jeroen
10:35 queued 04:37
created

VotingBundle/Services/RepositoryResolver.php (4 issues)

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\VotingBundle\Services;
4
5
use Doctrine\ORM\EntityManager;
6
use Kunstmaan\VotingBundle\Event\Facebook\FacebookLikeEvent;
7
use Kunstmaan\VotingBundle\Event\Facebook\FacebookSendEvent;
8
use Kunstmaan\VotingBundle\Event\LinkedIn\LinkedInShareEvent;
9
use Kunstmaan\VotingBundle\Event\UpDown\DownVoteEvent;
10
use Kunstmaan\VotingBundle\Event\UpDown\UpVoteEvent;
11
12
/**
13
 * Helper class get repository for an event
14
 */
15
class RepositoryResolver
16
{
17
    /**
18
     * Entity manager
19
     */
20
    protected $em;
21
22
    /**
23
     * Constructor
24
     *
25
     * @param object $em entity manager
0 ignored issues
show
Consider making the type for parameter $em a bit more specific; maybe use EntityManager.
Loading history...
26
     */
27 5
    public function __construct(EntityManager $em)
0 ignored issues
show
You have injected the EntityManager via parameter $em. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
28
    {
29 5
        $this->em = $em;
30 5
    }
31
32
    /**
33
     * Return repository for event
34
     *
35
     * @param Event $event event
36
     *
37
     * @return Repository
0 ignored issues
show
Should the return type not be \Doctrine\Persistence\ObjectRepository|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
38
     */
39 5
    public function getRepositoryForEvent($event)
40
    {
41 5
        $repository = null;
42
43 5
        if ($event instanceof DownVoteEvent) {
44 1
            $repository = $this->getRepository('Kunstmaan\VotingBundle\Entity\UpDown\DownVote');
45
        }
46
47 5
        if ($event instanceof UpVoteEvent) {
48 1
            $repository = $this->getRepository('Kunstmaan\VotingBundle\Entity\UpDown\UpVote');
49
        }
50
51 5
        if ($event instanceof LinkedInShareEvent) {
52 1
            $repository = $this->getRepository('Kunstmaan\VotingBundle\Entity\LinkedIn\LinkedInShare');
53
        }
54
55 5
        if ($event instanceof FacebookLikeEvent) {
56 1
            $repository = $this->getRepository('Kunstmaan\VotingBundle\Entity\Facebook\FacebookLike');
57
        }
58
59 5
        if ($event instanceof FacebookSendEvent) {
60 1
            $repository = $this->getRepository('Kunstmaan\VotingBundle\Entity\Facebook\FacebookSend');
61
        }
62
63 5
        return $repository;
64
    }
65
66
    /**
67
     * Return a repository By name
68
     *
69
     * @param string $name name
70
     *
71
     * @return Repository
0 ignored issues
show
Should the return type not be \Doctrine\Persistence\ObjectRepository?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
72
     */
73 5
    protected function getRepository($name)
74
    {
75 5
        return $this->em->getRepository($name);
76
    }
77
}
78