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\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 |
||
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
|
|||
25 | { |
||
26 | $this->em = $em; |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * @return array |
||
0 ignored issues
–
show
|
|||
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'))), |
||
36 | new \Twig_SimpleFunction('getpageparts', array('needs_environment' => true, $this, 'getPageParts')), |
||
37 | new \Twig_SimpleFunction('has_page_parts', [$this, 'hasPageParts']), |
||
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
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 | $pageparts = $entityRepository->getPageParts($page, $context); |
||
75 | |||
76 | return $pageparts; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @param HasPagePartsInterface $page |
||
81 | * @param string $context |
||
82 | * |
||
83 | * @return bool |
||
84 | */ |
||
85 | public function hasPageParts(HasPagePartsInterface $page, $context = 'main') |
||
86 | { |
||
87 | return $this->em->getRepository(PagePartRef::class)->hasPageParts($page, $context); |
||
88 | } |
||
89 | } |
||
90 |
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:If that code throws an exception and the
EntityManager
is closed. Any other code which depends on the same instance of theEntityManager
during this request will fail.On the other hand, if you instead inject the
ManagerRegistry
, thegetManager()
method guarantees that you will always get a usable manager instance.