Failed Conditions
Pull Request — experimental/3.1 (#2199)
by chihiro
82:14 queued 75:34
created

DbalExecutor   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A execute() 0 9 3
1
<?php
2
3
namespace Eccube\Doctrine\Common\CsvDataFixtures\Executor;
4
5
use Doctrine\Common\DataFixtures\Executor\AbstractExecutor;
6
use Doctrine\ORM\EntityManager;
7
8
/**
9
 * Doctrine Dbal を使用した Executor.
10
 *
11
 * @see https://github.com/doctrine/data-fixtures/blob/master/lib/Doctrine/Common/DataFixtures/Executor/AbstractExecutor.php
12
 * @see https://gist.github.com/gskema/a182aaf7cc04001aebba9c1aad86b40b
13
 */
14
class DbalExecutor extends AbstractExecutor
15
{
16
    /** @var $em \Doctrine\ORM\EntityManager */
17
    protected $em;
18
19
    public function __construct(EntityManager $em)
0 ignored issues
show
Bug introduced by
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...
introduced by
Missing function doc comment
Loading history...
20
    {
21
        $this->em = $em;
22
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function execute(array $fixtures, $append = false)
28
    {
29
        if ($append) {
30
            trigger_error('$append parameter is not supported.', E_USER_WARNING);
31
        }
32
        foreach ($fixtures as $CsvFixture) {
33
            $CsvFixture->load($this->em);
34
        }
35
    }
36
}
37