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
Documentation
introduced
by
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 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 On the other hand, if you instead inject the
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
|
|||
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
|
|||
72 | */ |
||
73 | 5 | protected function getRepository($name) |
|
74 | { |
||
75 | 5 | return $this->em->getRepository($name); |
|
76 | } |
||
77 | } |
||
78 |