Completed
Push — master ( 6d6774...64f3ed )
by Jeroen
11:23 queued 05:13
created

ArticleBundle/Twig/ArticleTwigExtension.php (5 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\Routing\Generator\UrlGeneratorInterface;
8
use Symfony\Component\Routing\RouterInterface;
9
use Twig\Extension\AbstractExtension;
10
use Twig\TwigFunction;
11
12
/**
13
 * Extension for article bundle.
14
 *
15
 * @final since 5.4
16
 */
17
class ArticleTwigExtension extends AbstractExtension
18
{
19
    /**
20
     * @var EntityManagerInterface
21
     */
22
    private $em;
23
24
    /**
25
     * @var RouterInterface
26
     */
27
    private $router;
28
29
    /**
30
     * ArticleTwigExtension constructor.
31
     *
32
     * @param EntityManagerInterface $em
33
     * @param RouterInterface        $router
34
     */
35
    public function __construct(EntityManagerInterface $em, RouterInterface $router)
36
    {
37
        $this->em = $em;
38
        $this->router = $router;
39
40
        if (\func_num_args() > 2) {
41
            @trigger_error(sprintf('Passing the "request_stack" service as the third argument in "%s" is deprecated in KunstmaanArticleBundle 5.1 and will be removed in KunstmaanArticleBundle 6.0. Remove the "request_stack" argument from your service definition.', __METHOD__), E_USER_DEPRECATED);
42
        }
43
    }
44
45
    /**
46
     * Returns a list of functions to add to the existing list.
47
     *
48
     * @return array An array of functions
0 ignored issues
show
Consider making the return type a bit more specific; maybe use TwigFunction[].

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...
49
     */
50
    public function getFunctions()
51
    {
52
        return array(
53
            new TwigFunction(
54
                'get_article_tag_path', array($this, 'getArticleTagRouterPath')
55
            ),
56
            new TwigFunction(
57
                'get_article_category_path', array($this, 'getArticleCategoryRouterPath')
58
            ),
59
            new TwigFunction(
60
                'get_article_categories', array($this, 'getCategories')
61
            ),
62
            new TwigFunction(
63
                'get_article_tags', array($this, 'getTags')
64
            ),
65
        );
66
    }
67
68
    /**
69
     * Get tags array for view.
70
     *
71
     * @param Request $request
72
     * @param string  $className
73
     *
74
     * @return array
0 ignored issues
show
Consider making the return type a bit more specific; maybe use array<string,boolean|array>.

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

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
129
        return $this->getArticleRouterPath($routeName, 'tag', $slug, $tag, $locale, $parameters, $referenceType);
130
    }
131
132
    /**
133
     * @param string $slug
134
     * @param string $category
135
     * @param string $locale
136
     * @param array  $parameters
137
     * @param int    $referenceType
138
     *
139
     * @return string
140
     */
141
    public function getArticleCategoryRouterPath($slug, $category, $locale, $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
142
    {
143
        $routeName = sprintf('_slug_category_%s', $locale);
144
145
        return $this->getArticleRouterPath($routeName, 'category', $slug, $category, $locale, $parameters, $referenceType);
146
    }
147
148
    /**
149
     * @return string
150
     */
151
    public function getName()
152
    {
153
        return 'article_twig_extension';
154
    }
155
156
    /**
157
     * @param string $routeName
158
     * @param string $type
159
     * @param string $slug
160
     * @param string $tagOrCategory
161
     * @param string $locale
162
     * @param array  $parameters
163
     * @param int    $referenceType
164
     *
165
     * @return string
166
     */
167
    protected function getArticleRouterPath($routeName, $type, $slug, $tagOrCategory, $locale, $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
168
    {
169
        if (!$this->articleRouteExists($type, $locale)) {
170
            $routeName = '_slug';
171
        }
172
        if (!isset($parameters[$type])) {
173
            $parameters[$type] = $tagOrCategory;
174
        }
175
        if (!isset($parameters['url'])) {
176
            $parameters['url'] = $slug;
177
        }
178
        if (!isset($parameters['_locale'])) {
179
            $parameters['_locale'] = $locale;
180
        }
181
182
        return $this->router->generate($routeName, $parameters, $referenceType);
183
    }
184
185
    /**
186
     * @param string $type
187
     * @param string $locale
188
     *
189
     * @return bool
190
     */
191
    protected function articleRouteExists($type, $locale)
192
    {
193
        $routeName = sprintf('_slug_%s_%s', $type, $locale);
194
195
        try {
196
            return !\is_null($this->router->getRouteCollection()->get($routeName));
197
        } catch (\Exception $e) {
198
            return false;
199
        }
200
    }
201
}
202