Completed
Push — master ( 770316...74fc07 )
by Jeroen
09:08 queued 02:44
created

NodeTwigExtension   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 258
Duplicated Lines 7.75 %

Coupling/Cohesion

Components 2
Dependencies 10

Importance

Changes 0
Metric Value
wmc 20
lcom 2
cbo 10
dl 20
loc 258
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getNodeTranslationByNodeId() 0 6 1
A getPageByNodeTranslation() 0 4 1
A getNodeFor() 0 4 1
A getNodeTranslationFor() 0 4 1
A getNodeByInternalName() 0 10 2
A getPathByInternalName() 10 10 2
A getUrlByInternalName() 10 10 2
A getNodeMenu() 0 11 2
A isStructureNode() 0 4 1
A fileExists() 0 4 1
A getRouteParametersByInternalName() 0 18 2
A __construct() 0 11 1
A getFunctions() 0 48 1
A getOverviewRoute() 0 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\StructureNode;
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', array($this, 'getNodeFor')
69
            ),
70
            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...
71
                'get_node_translation_for',
72
                array($this, 'getNodeTranslationFor')
73
            ),
74
            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...
75
                'get_node_by_internal_name',
76
                array($this, 'getNodeByInternalName')
77
            ),
78
            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...
79
                'get_url_by_internal_name',
80
                array($this, 'getUrlByInternalName')
81
            ),
82
            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...
83
                'get_path_by_internal_name',
84
                array($this, 'getPathByInternalName')
85
            ),
86
            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...
87
                'get_page_by_node_translation',
88
                array($this, 'getPageByNodeTranslation')
89
            ),
90
            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...
91
                'get_node_menu',
92
                array($this, 'getNodeMenu')
93
            ),
94
            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...
95
                'is_structure_node',
96
                array($this, 'isStructureNode')
97
            ),
98
            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...
99
                'file_exists',
100
                array($this, 'fileExists')
101
            ),
102
            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...
103
                'get_node_trans_by_node_id',
104
                array($this, 'getNodeTranslationByNodeId')
105
            ),
106
            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...
107
                'getOverviewRoute',
108
                array($this, 'getOverviewRoute')
109
            ),
110
        );
111
    }
112
113
    /**
114
     * Get the node translation object based on node id and language.
115
     *
116
     * @param int    $nodeId
117
     * @param string $lang
118
     *
119
     * @return NodeTranslation
120
     */
121
    public function getNodeTranslationByNodeId($nodeId, $lang)
122
    {
123
        $repo = $this->em->getRepository('KunstmaanNodeBundle:NodeTranslation');
124
125
        return $repo->getNodeTranslationByNodeIdQueryBuilder($nodeId, $lang);
126
    }
127
128
    /**
129
     * @param NodeTranslation $nodeTranslation
130
     *
131
     * @return null|object
132
     */
133
    public function getPageByNodeTranslation(NodeTranslation $nodeTranslation)
134
    {
135
        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...
136
    }
137
138
    /**
139
     * @param PageInterface $page
140
     *
141
     * @return Node
142
     */
143
    public function getNodeFor(PageInterface $page)
144
    {
145
        return $this->em->getRepository('KunstmaanNodeBundle:Node')->getNodeFor($page);
146
    }
147
148
    /**
149
     * @param PageInterface $page
150
     *
151
     * @return NodeTranslation
152
     */
153
    public function getNodeTranslationFor(PageInterface $page)
154
    {
155
        return $this->em->getRepository('KunstmaanNodeBundle:NodeTranslation')->getNodeTranslationFor($page);
156
    }
157
158
    /**
159
     * @param string $internalName
160
     * @param string $locale
161
     *
162
     * @return Node|null
163
     */
164
    public function getNodeByInternalName($internalName, $locale)
165
    {
166
        $nodes = $this->em->getRepository('KunstmaanNodeBundle:Node')
167
            ->getNodesByInternalName($internalName, $locale);
168
        if (!empty($nodes)) {
169
            return $nodes[0];
170
        }
171
172
        return null;
173
    }
174
175
    /**
176
     * @param string $internalName Internal name of the node
177
     * @param string $locale       Locale
178
     * @param array  $parameters   (optional) extra parameters
179
     * @param bool   $relative     (optional) return relative path?
180
     *
181
     * @return string
182
     */
183 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...
184
    {
185
        $routeParameters = $this->getRouteParametersByInternalName($internalName, $locale, $parameters);
186
187
        return $this->generator->generate(
188
            '_slug',
189
            $routeParameters,
190
            $relative ? UrlGeneratorInterface::RELATIVE_PATH : UrlGeneratorInterface::ABSOLUTE_PATH
191
        );
192
    }
193
194
    /**
195
     * @param string $internalName   Internal name of the node
196
     * @param string $locale         Locale
197
     * @param array  $parameters     (optional) extra parameters
198
     * @param bool   $schemeRelative (optional) return relative scheme?
199
     *
200
     * @return string
201
     */
202 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...
203
    {
204
        $routeParameters = $this->getRouteParametersByInternalName($internalName, $locale, $parameters);
205
206
        return $this->generator->generate(
207
            '_slug',
208
            $routeParameters,
209
            $schemeRelative ? UrlGeneratorInterface::NETWORK_PATH : UrlGeneratorInterface::ABSOLUTE_URL
210
        );
211
    }
212
213
    /**
214
     * @param string $locale
215
     * @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...
216
     * @param bool   $includeHiddenFromNav
217
     *
218
     * @return NodeMenu
219
     */
220
    public function getNodeMenu($locale, Node $node = null, $includeHiddenFromNav = false)
221
    {
222
        $request = $this->requestStack->getMasterRequest();
223
        $isPreview = $request->attributes->has('preview') && $request->attributes->get('preview') === true;
224
        $this->nodeMenu->setLocale($locale);
225
        $this->nodeMenu->setCurrentNode($node);
226
        $this->nodeMenu->setIncludeOffline($isPreview);
227
        $this->nodeMenu->setIncludeHiddenFromNav($includeHiddenFromNav);
228
229
        return $this->nodeMenu;
230
    }
231
232
    public function isStructureNode($page)
233
    {
234
        return $page instanceof StructureNode;
235
    }
236
237
    public function fileExists($filename)
238
    {
239
        return file_exists($filename);
240
    }
241
242
    /**
243
     * @param string $internalName
244
     * @param string $locale
245
     * @param array  $parameters
246
     *
247
     * @return array
248
     */
249
    private function getRouteParametersByInternalName($internalName, $locale, $parameters = array())
250
    {
251
        $url = '';
252
        $translation = $this->em->getRepository('KunstmaanNodeBundle:NodeTranslation')
253
            ->getNodeTranslationByLanguageAndInternalName($locale, $internalName);
254
255
        if (!\is_null($translation)) {
256
            $url = $translation->getUrl();
257
        }
258
259
        return array_merge(
260
            array(
261
                'url' => $url,
262
                '_locale' => $locale,
263
            ),
264
            $parameters
265
        );
266
    }
267
268
    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...
269
    {
270
        if ($node instanceof OverviewNavigationInterface) {
271
            return $node->getOverViewRoute();
272
        }
273
274
        return null;
275
    }
276
}
277