Completed
Pull Request — 5.2 (#2400)
by
unknown
17:37 queued 05:03
created

NodeTwigExtension::isStructurePage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Kunstmaan\NodeBundle\Twig;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Kunstmaan\AdminListBundle\Entity\OverviewNavigationInterface;
7
use Kunstmaan\NodeBundle\Entity\Node;
8
use Kunstmaan\NodeBundle\Entity\NodeTranslation;
9
use Kunstmaan\NodeBundle\Entity\PageInterface;
10
use Kunstmaan\NodeBundle\Entity\AbstractStructurePage;
11
use Kunstmaan\NodeBundle\Helper\NodeMenu;
12
use Symfony\Component\HttpFoundation\RequestStack;
13
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
14
use Twig_Extension;
15
16
/**
17
 * Extension to fetch node / translation by page in Twig templates
18
 */
19
class NodeTwigExtension 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...
20
{
21
    /**
22
     * @var EntityManagerInterface
23
     */
24
    private $em;
25
26
    /**
27
     * @var UrlGeneratorInterface
28
     */
29
    private $generator;
30
31
    /**
32
     * @var NodeMenu
33
     */
34
    private $nodeMenu;
35
36
    /**
37
     * @var RequestStack
38
     */
39
    private $requestStack;
40
41
    /**
42
     * @param \Doctrine\ORM\EntityManagerInterface                       $em
43
     * @param \Symfony\Component\Routing\Generator\UrlGeneratorInterface $generator
44
     * @param \Kunstmaan\NodeBundle\Helper\NodeMenu                      $nodeMenu
45
     * @param \Symfony\Component\HttpFoundation\RequestStack             $requestStack
46
     */
47
    public function __construct(
48
        EntityManagerInterface $em,
49
        UrlGeneratorInterface $generator,
50
        NodeMenu $nodeMenu,
51
        RequestStack $requestStack
52
    ) {
53
        $this->em = $em;
54
        $this->generator = $generator;
55
        $this->nodeMenu = $nodeMenu;
56
        $this->requestStack = $requestStack;
57
    }
58
59
    /**
60
     * Returns a list of functions to add to the existing list.
61
     *
62
     * @return array An array of functions
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...
63
     */
64
    public function getFunctions()
65
    {
66
        return array(
67
            new \Twig_SimpleFunction(
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...
68
                'get_node_for',
69
                array($this, 'getNodeFor')
70
            ),
71
            new \Twig_SimpleFunction(
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...
72
                'get_node_translation_for',
73
                array($this, 'getNodeTranslationFor')
74
            ),
75
            new \Twig_SimpleFunction(
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...
76
                'get_node_by_internal_name',
77
                array($this, 'getNodeByInternalName')
78
            ),
79
            new \Twig_SimpleFunction(
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...
80
                'get_url_by_internal_name',
81
                array($this, 'getUrlByInternalName')
82
            ),
83
            new \Twig_SimpleFunction(
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...
84
                'get_path_by_internal_name',
85
                array($this, 'getPathByInternalName')
86
            ),
87
            new \Twig_SimpleFunction(
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...
88
                'get_page_by_node_translation',
89
                array($this, 'getPageByNodeTranslation')
90
            ),
91
            new \Twig_SimpleFunction(
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...
92
                'get_node_menu',
93
                array($this, 'getNodeMenu')
94
            ),
95
            new \Twig_SimpleFunction(
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...
96
                'is_structure_node',
97
                array($this, 'isStructureNode')
98
            ),
99
            new \Twig_SimpleFunction(
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...
100
                'is_structure_page',
101
                array($this, 'isStructurePage')
102
            ),
103
            new \Twig_SimpleFunction(
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...
104
                'file_exists',
105
                array($this, 'fileExists')
106
            ),
107
            new \Twig_SimpleFunction(
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...
108
                'get_node_trans_by_node_id',
109
                array($this, 'getNodeTranslationByNodeId')
110
            ),
111
            new \Twig_SimpleFunction(
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...
112
                'getOverviewRoute',
113
                array($this, 'getOverviewRoute')
114
            ),
115
        );
116
    }
117
118
    /**
119
     * Get the node translation object based on node id and language.
120
     *
121
     * @param int    $nodeId
122
     * @param string $lang
123
     *
124
     * @return NodeTranslation
125
     */
126
    public function getNodeTranslationByNodeId($nodeId, $lang)
127
    {
128
        $repo = $this->em->getRepository('KunstmaanNodeBundle:NodeTranslation');
129
130
        return $repo->getNodeTranslationByNodeIdQueryBuilder($nodeId, $lang);
131
    }
132
133
    /**
134
     * @param NodeTranslation $nodeTranslation
135
     *
136
     * @return null|object
137
     */
138
    public function getPageByNodeTranslation(NodeTranslation $nodeTranslation)
139
    {
140
        return $nodeTranslation->getRef($this->em);
0 ignored issues
show
Compatibility introduced by
$this->em of type object<Doctrine\ORM\EntityManagerInterface> is not a sub-type of object<Doctrine\ORM\EntityManager>. It seems like you assume a concrete implementation of the interface Doctrine\ORM\EntityManagerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
141
    }
142
143
    /**
144
     * @param PageInterface $page
145
     *
146
     * @return Node
147
     */
148
    public function getNodeFor(PageInterface $page)
149
    {
150
        return $this->em->getRepository('KunstmaanNodeBundle:Node')->getNodeFor($page);
151
    }
152
153
    /**
154
     * @param PageInterface $page
155
     *
156
     * @return NodeTranslation
157
     */
158
    public function getNodeTranslationFor(PageInterface $page)
159
    {
160
        return $this->em->getRepository('KunstmaanNodeBundle:NodeTranslation')->getNodeTranslationFor($page);
161
    }
162
163
    /**
164
     * @param string $internalName
165
     * @param string $locale
166
     *
167
     * @return Node|null
168
     */
169
    public function getNodeByInternalName($internalName, $locale)
170
    {
171
        $nodes = $this->em->getRepository('KunstmaanNodeBundle:Node')
172
            ->getNodesByInternalName($internalName, $locale);
173
        if (!empty($nodes)) {
174
            return $nodes[0];
175
        }
176
177
        return null;
178
    }
179
180
    /**
181
     * @param string $internalName Internal name of the node
182
     * @param string $locale       Locale
183
     * @param array  $parameters   (optional) extra parameters
184
     * @param bool   $relative     (optional) return relative path?
185
     *
186
     * @return string
187
     */
188 View Code Duplication
    public function getPathByInternalName($internalName, $locale, $parameters = array(), $relative = false)
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...
189
    {
190
        $routeParameters = $this->getRouteParametersByInternalName($internalName, $locale, $parameters);
191
192
        return $this->generator->generate(
193
            '_slug',
194
            $routeParameters,
195
            $relative ? UrlGeneratorInterface::RELATIVE_PATH : UrlGeneratorInterface::ABSOLUTE_PATH
196
        );
197
    }
198
199
    /**
200
     * @param string $internalName   Internal name of the node
201
     * @param string $locale         Locale
202
     * @param array  $parameters     (optional) extra parameters
203
     * @param bool   $schemeRelative (optional) return relative scheme?
204
     *
205
     * @return string
206
     */
207 View Code Duplication
    public function getUrlByInternalName($internalName, $locale, $parameters = array(), $schemeRelative = false)
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...
208
    {
209
        $routeParameters = $this->getRouteParametersByInternalName($internalName, $locale, $parameters);
210
211
        return $this->generator->generate(
212
            '_slug',
213
            $routeParameters,
214
            $schemeRelative ? UrlGeneratorInterface::NETWORK_PATH : UrlGeneratorInterface::ABSOLUTE_URL
215
        );
216
    }
217
218
    /**
219
     * @param string $locale
220
     * @param Node   $node
0 ignored issues
show
Documentation introduced by
Should the type for parameter $node not be null|Node?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
221
     * @param bool   $includeHiddenFromNav
222
     *
223
     * @return NodeMenu
224
     */
225
    public function getNodeMenu($locale, Node $node = null, $includeHiddenFromNav = false)
226
    {
227
        $request = $this->requestStack->getMasterRequest();
228
        $isPreview = $request->attributes->has('preview') && $request->attributes->get('preview') === true;
229
        $this->nodeMenu->setLocale($locale);
230
        $this->nodeMenu->setCurrentNode($node);
231
        $this->nodeMenu->setIncludeOffline($isPreview);
232
        $this->nodeMenu->setIncludeHiddenFromNav($includeHiddenFromNav);
233
234
        return $this->nodeMenu;
235
    }
236
237
    /**
238
     * @deprecated Using the isStructureNode method is deprecated in KunstmaanNodeBundle 5.2 and will be removed in KunstmaanNodeBundle 6.0. use isStructurePage.
239
     */
240
    public function isStructureNode($page)
241
    {
242
        return $page instanceof AbstractStructurePage;
243
    }
244
245
    public function isStructurePage($page)
246
    {
247
        return $page instanceof AbstractStructurePage;
248
    }
249
250
    public function fileExists($filename)
251
    {
252
        return file_exists($filename);
253
    }
254
255
    /**
256
     * @param string $internalName
257
     * @param string $locale
258
     * @param array  $parameters
259
     *
260
     * @return array
261
     */
262
    private function getRouteParametersByInternalName($internalName, $locale, $parameters = array())
263
    {
264
        $url = '';
265
        $translation = $this->em->getRepository('KunstmaanNodeBundle:NodeTranslation')
266
            ->getNodeTranslationByLanguageAndInternalName($locale, $internalName);
267
268
        if (!is_null($translation)) {
269
            $url = $translation->getUrl();
270
        }
271
272
        return array_merge(
273
            array(
274
                'url' => $url,
275
                '_locale' => $locale,
276
            ),
277
            $parameters
278
        );
279
    }
280
281
    public function getOverviewRoute($node)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
282
    {
283
        if ($node instanceof OverviewNavigationInterface) {
284
            return $node->getOverViewRoute();
285
        }
286
287
        return null;
288
    }
289
}
290