Completed
Pull Request — 5.0 (#2162)
by Kevin
14:33
created

ArticleBundle/Twig/ArticleTwigExtension.php (4 issues)

Upgrade to new PHP Analysis Engine

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\ArticleBundle\Twig;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Symfony\Component\HttpFoundation\Request;
7
use Symfony\Component\HttpFoundation\RequestStack;
8
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
9
use Symfony\Component\Routing\RouterInterface;
10
use Twig_Extension;
11
12
/**
13
 * Extension for article bundle.
14
 */
15
class ArticleTwigExtension extends Twig_Extension
16
{
17
    /**
18
     * @var EntityManagerInterface $em
19
     */
20
    private $em;
21
22
    /**
23
     * @var RouterInterface
24
     */
25
    private $router;
26
27
    /**
28
     * @var RequestStack
29
     */
30
    private $requestStack;
31
32
    /**
33
     * ArticleTwigExtension constructor.
34
     *
35
     * @param EntityManagerInterface $em
36
     * @param RouterInterface        $router
37
     * @param RequestStack           $requestStack
38
     */
39
    public function __construct(EntityManagerInterface $em, RouterInterface $router, RequestStack $requestStack) {
40
        $this->em = $em;
41
        $this->router = $router;
42
        $this->requestStack = $requestStack;
43
    }
44
45
    /**
46
     * Returns a list of functions to add to the existing list.
47
     *
48
     * @return array An array of functions
49
     */
50
    public function getFunctions()
51
    {
52
        return array(
53
            new \Twig_SimpleFunction(
54
                'get_article_tag_path', array($this, 'getArticleTagRouterPath')
55
            ),
56
            new \Twig_SimpleFunction(
57
                'get_article_category_path', array($this, 'getArticleCategoryRouterPath')
58
            ),
59
            new \Twig_SimpleFunction(
60
                'get_article_categories', array($this, 'getCategories')
61
            ),
62
            new \Twig_SimpleFunction(
63
                'get_article_tags', array($this, 'getTags')
64
            ),
65
        );
66
    }
67
68
69
    /**
70
     * Get tags array for view.
71
     *
72
     * @param Request $request
73
     * @param string  $className
74
     *
75
     * @return array
0 ignored issues
show
Consider making the return type a bit more specific; maybe use array<string,array|boolean>.

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...
76
     */
77 View Code Duplication
    public function getTags(Request $request, $className)
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...
78
    {
79
        $context = array();
80
81
        $tagRepository = $this->em->getRepository($className);
82
        $context['tags'] = $tagRepository->findBy(array(), array('name' => 'ASC'));
83
84
        $searchTag = $request->get('tag') ? explode(',', $request->get('tag')) : null;
85
        if ($searchTag) {
86
            $context['activeTag'] = true;
87
            $context['activeTags'] = $searchTag;
88
        }
89
90
        return $context;
91
    }
92
93
    /**
94
     * Get categories array for view.
95
     *
96
     * @param Request $request
97
     * @param string  $className
98
     * @return array
0 ignored issues
show
Consider making the return type a bit more specific; maybe use array<string,array|boolean>.

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...
99
     */
100 View Code Duplication
    public function getCategories(Request $request, $className)
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...
101
    {
102
        $context = array();
103
104
        $categoryRepository = $this->em->getRepository($className);
105
        $context['categories'] = $categoryRepository->findBy(array(), array('name' => 'ASC'));
106
107
        $searchCategory = $request->get('category') ? explode(',', $request->get('category')) : null;
108
        if ($searchCategory) {
109
            $context['activeCategory'] = true;
110
            $context['activeCategories'] = $searchCategory;
111
        }
112
113
        return $context;
114
    }
115
116
    /**
117
     * @param string $slug
118
     * @param string $tag
119
     * @param string $locale
120
     * @param array  $parameters
121
     * @param int    $referenceType
122
     *
123
     * @return string
124
     */
125
    public function getArticleTagRouterPath($slug, $tag, $locale, $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
126
    {
127
        $routeName = sprintf('_slug_tag_%s', $locale);
128
        return $this->getArticleRouterPath($routeName, 'tag', $slug, $tag, $locale, $parameters, $referenceType);
129
    }
130
131
    /**
132
     * @param string $slug
133
     * @param string $category
134
     * @param string $locale
135
     * @param array  $parameters
136
     * @param int    $referenceType
137
     *
138
     * @return string
139
     */
140
    public function getArticleCategoryRouterPath($slug, $category, $locale, $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
141
    {
142
        $routeName = sprintf('_slug_category_%s', $locale);
143
        return $this->getArticleRouterPath($routeName, 'category', $slug, $category, $locale, $parameters, $referenceType);
144
    }
145
146
    /**
147
     * @return string
148
     */
149
    public function getName()
150
    {
151
        return 'article_twig_extension';
152
    }
153
154
    /**
155
     * @param string $routeName
156
     * @param string $type
157
     * @param string $slug
158
     * @param string $tagOrCategory
159
     * @param string $locale
160
     * @param array  $parameters
161
     * @param int    $referenceType
162
     *
163
     * @return string
164
     */
165
    protected function getArticleRouterPath($routeName, $type, $slug, $tagOrCategory, $locale, $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
166
    {
167
        if (!$this->articleRouteExists($type, $locale)) {
168
            $routeName = '_slug';
169
        }
170
        if (!isset($parameters[$type])) {
171
            $parameters[$type] = $tagOrCategory;
172
        }
173
        if (!isset($parameters['url'])) {
174
            $parameters['url'] = $slug;
175
        }
176
        if (!isset($parameters['_locale'])) {
177
            $parameters['_locale'] = $locale;
178
        }
179
        return $this->router->generate($routeName, $parameters, $referenceType);
180
    }
181
182
    /**
183
     * @param string $type
184
     * @param string $locale
185
     * @return bool
186
     */
187
    protected function articleRouteExists($type, $locale)
188
    {
189
        $routeName = sprintf('_slug_%s_%s', $type, $locale);
190
191
        try {
192
            return !is_null($this->router->getRouteCollection()->get($routeName));
193
        } catch (\Exception $e) {
194
            return false;
195
        }
196
    }
197
}
198