Completed
Push — master ( aba493...5356ed )
by Ruud
315:38 queued 305:00
created

FixturesBundle/Builder/PagePartBuilder.php (1 issue)

Upgrade to new PHP Analysis Engine

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\FixturesBundle\Builder;
4
5
use Doctrine\ORM\EntityManager;
6
use Kunstmaan\FixturesBundle\Loader\Fixture;
7
use Kunstmaan\FixturesBundle\Populator\Populator;
8
use Kunstmaan\PagePartBundle\Helper\PagePartInterface;
9
10
class PagePartBuilder implements BuilderInterface
11
{
12
    /** @var \Doctrine\ORM\EntityManager */
13
    private $em;
14
15
    /** @var \Kunstmaan\PagePartBundle\Repository\PagePartRefRepository */
16
    private $pagePartRepo;
17
18
    /** @var \Kunstmaan\FixturesBundle\Populator\Populator */
19
    private $populator;
20
21
    public function __construct(EntityManager $em, Populator $populator)
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 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...
22
    {
23
        $this->em = $em;
24
        $this->pagePartRepo = $em->getRepository('KunstmaanPagePartBundle:PagePartRef');
25
        $this->populator = $populator;
26
    }
27
28
    public function canBuild(Fixture $fixture)
29
    {
30
        if ($fixture->getEntity() instanceof PagePartInterface) {
31
            return true;
32
        }
33
34
        return false;
35
    }
36
37
    public function preBuild(Fixture $fixture)
38
    {
39
        return;
40
    }
41
42
    public function postBuild(Fixture $fixture)
43
    {
44
        return;
45
    }
46
47
    public function postFlushBuild(Fixture $fixture)
48
    {
49
        $params = $fixture->getParameters();
50
        $translations = $fixture->getTranslations();
51
        $original = $fixture->getEntity();
52
53 View Code Duplication
        if (!isset($params['page']) || empty($translations)) {
54
            throw new \Exception(
55
                'No page reference and/or translations detected for pagepart fixture ' . $fixture->getName() . ' (' . $fixture->getClass() . ')'
56
            );
57
        }
58
59
        $pageFixture = $params['page'];
60
        if (!$pageFixture instanceof Fixture) {
61
            throw new \Exception(
62
                'Could not find a reference "' . $params['page'] . '"" for fixture ' . $fixture->getName() . ' (' . $fixture->getClass() . ')'
63
            );
64
        }
65
66
        $additionalEntities = $pageFixture->getAdditionalEntities();
67
        $pp = $original;
68
        $first = null;
69
        foreach ($translations as $language => $data) {
70
            $key = $pageFixture->getName() . '_' . $language;
71
            if (!isset($additionalEntities[$key])) {
72
                continue;
73
            }
74
75
            if ($first !== null) {
76
                $pp = clone $original;
77
            }
78
79
            $page = $additionalEntities[$key];
80
            $this->populator->populate($pp, $data);
81
            $this->em->persist($pp);
82
            $this->em->flush($pp);
83
84
            // Find latest position.
85
            $position = array_key_exists('position', $params) ? $params['position'] : null;
86
            $context = isset($params['context']) ? $params['context'] : 'main';
87 View Code Duplication
            if (is_null($position)) {
88
                $pageParts = $this->pagePartRepo->getPagePartRefs($page, $context);
89
                $position = count($pageParts) + 1;
90
            }
91
92
            $this->pagePartRepo->addPagePart($page, $pp, $position, $context);
93
            $first = false;
94
        }
95
    }
96
}
97