Completed
Push — master ( ba8ed9...770316 )
by Jeroen
06:11
created

PagePartTwigExtension::renderPageParts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
dl 13
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 5
1
<?php
2
3
namespace Kunstmaan\PagePartBundle\Twig\Extension;
4
5
use Doctrine\ORM\EntityManager;
6
use Kunstmaan\PagePartBundle\Entity\PagePartRef;
7
use Kunstmaan\PagePartBundle\Helper\HasPagePartsInterface;
8
use Kunstmaan\PagePartBundle\Helper\PagePartInterface;
9
use Kunstmaan\PagePartBundle\Repository\PagePartRefRepository;
10
11
/**
12
 * PagePartTwigExtension
13
 */
14
class PagePartTwigExtension extends \Twig_Extension
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Extension has been deprecated with message: since Twig 2.7, use "Twig\Extension\AbstractExtension" instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
15
{
16
    /**
17
     * @var EntityManager
18
     */
19
    protected $em;
20
21
    /**
22
     * @param EntityManager $em
23
     */
24
    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...
25
    {
26
        $this->em = $em;
27
    }
28
29
    /**
30
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use \Twig_SimpleFunction[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
31
     */
32
    public function getFunctions()
33
    {
34
        return array(
35
            new \Twig_SimpleFunction('render_pageparts', array($this, 'renderPageParts'), array('needs_environment' => true, 'needs_context' => true, 'is_safe' => array('html'))),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: since Twig 2.7, use "Twig\TwigFunction" instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
36
            new \Twig_SimpleFunction('getpageparts', array('needs_environment' => true, $this, 'getPageParts')),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: since Twig 2.7, use "Twig\TwigFunction" instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
37
            new \Twig_SimpleFunction('has_page_parts', [$this, 'hasPageParts']),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: since Twig 2.7, use "Twig\TwigFunction" instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
38
        );
39
    }
40
41
    /**
42
     * @param \Twig_Environment     $env
43
     * @param array                 $twigContext The twig context
44
     * @param HasPagePartsInterface $page        The page
45
     * @param string                $contextName The pagepart context
46
     * @param array                 $parameters  Some extra parameters
47
     *
48
     * @return string
49
     */
50 View Code Duplication
    public function renderPageParts(\Twig_Environment $env, array $twigContext, HasPagePartsInterface $page, $contextName = 'main', array $parameters = array())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
    {
52
        $template = $env->loadTemplate('KunstmaanPagePartBundle:PagePartTwigExtension:widget.html.twig');
53
        /* @var $entityRepository PagePartRefRepository */
54
        $pageparts = $this->getPageParts($page, $contextName);
55
        $newTwigContext = array_merge($parameters, array(
56
            'pageparts' => $pageparts,
57
            'page' => $page,
58
        ));
59
        $newTwigContext = array_merge($newTwigContext, $twigContext);
60
61
        return $template->render($newTwigContext);
62
    }
63
64
    /**
65
     * @param HasPagePartsInterface $page    The page
66
     * @param string                $context The pagepart context
67
     *
68
     * @return PagePartInterface[]
69
     */
70
    public function getPageParts(HasPagePartsInterface $page, $context = 'main')
71
    {
72
        /** @var $entityRepository PagePartRefRepository */
73
        $entityRepository = $this->em->getRepository('KunstmaanPagePartBundle:PagePartRef');
74
75
        return $entityRepository->getPageParts($page, $context);
76
    }
77
78
    /**
79
     * @param HasPagePartsInterface $page
80
     * @param string                $context
81
     *
82
     * @return bool
83
     */
84
    public function hasPageParts(HasPagePartsInterface $page, $context = 'main')
85
    {
86
        return $this->em->getRepository(PagePartRef::class)->hasPageParts($page, $context);
87
    }
88
}
89