CMSHelper   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 2
dl 0
loc 45
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A nodeGetTranslation() 0 8 1
A createTranslation() 0 15 2
A getContentTypeFromNodeElementClass() 0 8 3
1
<?php
2
3
namespace Alpixel\Bundle\CMSBundle\Helper;
4
5
use Alpixel\Bundle\CMSBundle\Entity\Node;
6
use Doctrine\ORM\EntityManager;
7
8
class CMSHelper
9
{
10
    protected $entityManager;
11
    protected $contentTypes;
12
13
    public function __construct(EntityManager $entityManager, $contentTypes)
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...
14
    {
15
        $this->entityManager = $entityManager;
16
        $this->contentTypes = $contentTypes;
17
    }
18
19
    public function nodeGetTranslation(Node $node, $locale)
20
    {
21
        $node = $this->entityManager
22
                     ->getRepository('AlpixelCMSBundle:Node')
23
                     ->findTranslation($node, $locale);
24
25
        return $node;
26
    }
27
28
    public function createTranslation(Node $object, $locale)
29
    {
30
        if ($object->getTranslationSource() !== null) {
31
            $source = $object->getTranslationSource();
32
        } else {
33
            $source = $object;
34
        }
35
36
        $node = clone $object;
37
        $node->setLocale($locale);
38
        $node->setTranslationSource($source);
39
        $node->setTitle(sprintf('Version %s de la page "%s"', strtoupper($locale), $node->getTitle()));
40
41
        return $node;
42
    }
43
44
    public function getContentTypeFromNodeElementClass(Node $object)
45
    {
46
        foreach ($this->contentTypes as $contentType) {
47
            if ($contentType['class'] == get_class($object)) {
48
                return $contentType;
49
            }
50
        }
51
    }
52
}
53