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 |
||
26 | */ |
||
27 | public function __construct(EntityManager $em) |
||
0 ignored issues
–
show
|
|||
28 | { |
||
29 | $this->em = $em; |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * Return repository for event |
||
34 | * |
||
35 | * @param Event $event event |
||
36 | * |
||
37 | * @return Repository |
||
0 ignored issues
–
show
|
|||
38 | */ |
||
39 | public function getRepositoryForEvent($event) |
||
40 | { |
||
41 | $repository = null; |
||
42 | |||
43 | if ($event instanceof DownVoteEvent) { |
||
44 | $repository = $this->getRepository('Kunstmaan\VotingBundle\Entity\UpDown\DownVote'); |
||
45 | } |
||
46 | |||
47 | if ($event instanceof UpVoteEvent) { |
||
48 | $repository = $this->getRepository('Kunstmaan\VotingBundle\Entity\UpDown\UpVote'); |
||
49 | } |
||
50 | |||
51 | if ($event instanceof LinkedInShareEvent) { |
||
52 | $repository = $this->getRepository('Kunstmaan\VotingBundle\Entity\LinkedIn\LinkedInShare'); |
||
53 | } |
||
54 | |||
55 | if ($event instanceof FacebookLikeEvent) { |
||
56 | $repository = $this->getRepository('Kunstmaan\VotingBundle\Entity\Facebook\FacebookLike'); |
||
57 | } |
||
58 | |||
59 | if ($event instanceof FacebookSendEvent) { |
||
60 | $repository = $this->getRepository('Kunstmaan\VotingBundle\Entity\Facebook\FacebookSend'); |
||
61 | } |
||
62 | |||
63 | 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
|
|||
72 | */ |
||
73 | protected function getRepository($name) |
||
74 | { |
||
75 | return $this->em->getRepository($name); |
||
76 | } |
||
77 | } |
||
78 |
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:If that code throws an exception and the
EntityManager
is closed. Any other code which depends on the same instance of theEntityManager
during this request will fail.On the other hand, if you instead inject the
ManagerRegistry
, thegetManager()
method guarantees that you will always get a usable manager instance.