Completed
Push — master ( c02aa8...d4d075 )
by Benjamin
36:25 queued 06:32
created

CMSHelper::getNodeElementEntityFromNode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
namespace Alpixel\Bundle\CMSBundle\Helper;
4
5
use Alpixel\Bundle\CMSBundle\Entity\Node;
6
use Alpixel\Bundle\CMSBundle\Entity\NodeInterface;
7
use Doctrine\ORM\EntityManager;
8
9
class CMSHelper
10
{
11
    protected $entityManager;
12
    protected $contentTypes;
13
14
    public function __construct(EntityManager $entityManager, $contentTypes)
15
    {
16
        $this->entityManager = $entityManager;
17
        $this->contentTypes  = $contentTypes;
18
    }
19
20
    public function nodeGetTranslation(NodeInterface $node, $locale)
21
    {
22
        $node = $this->entityManager
23
                     ->getRepository('CMSBundle:Node')
24
                     ->findTranslation($node->getNode(), $locale);
0 ignored issues
show
Bug introduced by
The method getNode() does not seem to exist on object<Alpixel\Bundle\CM...e\Entity\NodeInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
25
26
        return $node;
27
    }
28
29
    public function createTranslation(Node $object, $locale)
30
    {
31
        if ($object->getTranslationSource() !== null) {
32
            $source = $object->getTranslationSource();
33
        } else {
34
            $source = $object;
35
        }
36
37
        $content = $this->getNodeElementEntityFromNode($object);
38
        $translatedContent = clone $content;
39
40
        $node = $translatedContent->getNode();
41
        $node->setLocale($locale);
42
        $node->setTranslationSource($source);
43
        $node->setTitle(sprintf('Version %s de la page "%s"', strtoupper($locale), $node->getTitle()));
44
45
        return $translatedContent;
46
    }
47
48
    public function getNodeElementEntityFromNode(Node $node)
49
    {
50
        if (array_key_exists($node->getType(), $this->contentTypes)) {
51
            $contentType = $this->contentTypes[$node->getType()];
52
53
            return $this->entityManager
54
                        ->getRepository($contentType['class'])
55
                        ->findOneByNode($node);
56
        }
57
    }
58
}
59