AbstractController::getEntityManager()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ConferenceTools\Tickets\Controller;
4
5
use Carnage\Cqrs\MessageBus\MessageBusInterface;
6
use Doctrine\ORM\EntityManager;
7
use ConferenceTools\Tickets\Domain\Service\Configuration;
8
use Zend\Mvc\Controller\AbstractActionController;
9
use ZfrStripe\Client\StripeClient;
10
11
/**
12
 * Class AbstractController
13
 * @package HirePower\Common\Mvc\Controller
14
 * @method \Zend\Form\Form commandForm(string $commandClass, array $additionalData = [], array $defaults = [])
15
 * @method \Carnage\Cqrs\Mvc\Controller\Plugin\Events events()
16
 */
17
abstract class AbstractController extends AbstractActionController
18
{
19
    /**
20
     * @var MessageBusInterface
21
     */
22
    private $commandBus;
23
24
    /**
25
     * @var EntityManager
26
     */
27
    private $entityManager;
28
29
    /**
30
     * @var StripeClient
31
     */
32
    private $stripeClient;
33
34
    /**
35
     * @var Configuration
36
     */
37
    private $configuration;
38
39
    /**
40
     * AbstractController constructor.
41
     * @param MessageBusInterface $commandBus
42
     * @param EntityManager $entityManager
43
     * @param StripeClient $stripeClient
44
     */
45
    public function __construct(
46
        MessageBusInterface $commandBus,
47
        EntityManager $entityManager,
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $entityManager. 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...
48
        StripeClient $stripeClient,
49
        Configuration $configuration
50
    ) {
51
        $this->commandBus = $commandBus;
52
        $this->entityManager = $entityManager;
53
        $this->stripeClient = $stripeClient;
54
        $this->configuration = $configuration;
55
    }
56
57
    /**
58
     * @return MessageBusInterface
59
     */
60
    public function getCommandBus()
61
    {
62
        return $this->commandBus;
63
    }
64
65
    /**
66
     * @return EntityManager
67
     */
68
    public function getEntityManager()
69
    {
70
        return $this->entityManager;
71
    }
72
73
    /**
74
     * @return StripeClient
75
     */
76
    public function getStripeClient(): StripeClient
77
    {
78
        return $this->stripeClient;
79
    }
80
81
    /**
82
     * @return Configuration
83
     */
84
    public function getConfiguration(): Configuration
85
    {
86
        return $this->configuration;
87
    }
88
}