Completed
Push — master ( 440ed8...3092e4 )
by John
01:58
created

EntityManagedEndpointFactory   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 2
dl 0
loc 22
c 0
b 0
f 0
ccs 8
cts 8
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getLoggingEndpoint() 0 6 1
getEntityManagedEndpoint() 0 1 ?
1
<?php
2
namespace LunixRESTBasics\Endpoint;
3
4
use Doctrine\ORM\EntityManager;
5
use LunixREST\Endpoint\LoggingEndpoint;
6
use LunixREST\Endpoint\LoggingEndpointFactory;
7
use Psr\Log\LoggerInterface;
8
9
abstract class EntityManagedEndpointFactory extends LoggingEndpointFactory
10
{
11
    /**
12
     * @var EntityManager
13
     */
14
    protected $entityManager;
15
16 1
    public function __construct(EntityManager $entityManager, LoggerInterface $logger)
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...
17
    {
18 1
        $this->entityManager = $entityManager;
19 1
        parent::__construct($logger);
20 1
    }
21
22 1
    protected function getLoggingEndpoint(string $name, string $version): LoggingEndpoint
23
    {
24 1
        $endpoint = $this->getEntityManagedEndpoint($name, $version);
25 1
        $endpoint->setEntityManager($this->entityManager);
26 1
        return $endpoint;
27
    }
28
29
    protected abstract function getEntityManagedEndpoint(string $name, string $version): EntityManagedEndpoint;
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
30
}
31