Failed Conditions
Pull Request — experimental/3.1 (#2222)
by chihiro
53:42
created

DbalExecutor::execute()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 4
nop 2
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
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