Completed
Push — master ( ae5e03...0447ee )
by Jeroen
10:35 queued 04:37
created

src/Kunstmaan/MenuBundle/Service/MenuService.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\MenuBundle\Service;
4
5
use Doctrine\ORM\EntityManager;
6
use Kunstmaan\AdminBundle\Helper\DomainConfigurationInterface;
7
8
class MenuService
9
{
10
    /**
11
     * @var array
12
     */
13
    private $menuNames;
14
15
    /**
16
     * @var DomainConfigurationInterface
17
     */
18
    private $domainConfiguration;
19
20
    /**
21
     * @var EntityManager
22
     */
23
    private $em;
24
25
    /**
26
     * @var string
27
     */
28
    private $menuEntityClass;
29
30
    /**
31
     * @param array                        $menuNames
32
     * @param DomainConfigurationInterface $domainConfiguration
33
     * @param EntityManager                $em
34
     */
35
    public function __construct(array $menuNames, DomainConfigurationInterface $domainConfiguration, EntityManager $em, $menuEntityClass)
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...
36
    {
37
        $this->menuNames = $menuNames;
38
        $this->domainConfiguration = $domainConfiguration;
39
        $this->em = $em;
40
        $this->menuEntityClass = $menuEntityClass;
41
    }
42
43
    /**
44
     * Make sure the menu objects exist in the database for each locale.
45
     */
46
    public function makeSureMenusExist()
47
    {
48
        $locales = array_unique($this->getLocales());
49
        $required = array();
50
51
        foreach ($this->menuNames as $name) {
52
            $required[$name] = $locales;
53
        }
54
55
        $menuObjects = $this->em->getRepository($this->menuEntityClass)->findAll();
56
57
        foreach ($menuObjects as $menu) {
58
            if (\array_key_exists($menu->getName(), $required)) {
59
                $index = array_search($menu->getLocale(), $required[$menu->getName()]);
60
                if ($index !== false) {
61
                    unset($required[$menu->getName()][$index]);
62
                }
63
            }
64
        }
65
66
        foreach ($required as $name => $locales) {
67
            foreach ($locales as $locale) {
68
                $className = $this->menuEntityClass;
69
                $menu = new $className();
70
                $menu->setName($name);
71
                $menu->setLocale($locale);
72
                $this->em->persist($menu);
73
            }
74
        }
75
76
        $this->em->flush();
77
    }
78
79
    /**
80
     * @return array
81
     */
82
    private function getLocales()
83
    {
84
        return $this->domainConfiguration->getBackendLocales();
85
    }
86
}
87