Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

ArticleBundle/Twig/ArticleTwigExtension.php (2 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
    public function __construct(EntityManagerInterface $em, RouterInterface $router)
33
    {
34
        $this->em = $em;
35
        $this->router = $router;
36
37
        if (\func_num_args() > 2) {
38
            @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);
39
        }
40
    }
41
42
    /**
43
     * Returns a list of functions to add to the existing list.
44
     *
45
     * @return array An array of functions
46
     */
47
    public function getFunctions()
48
    {
49
        return [
50
            new TwigFunction(
51
                'get_article_tag_path', [$this, 'getArticleTagRouterPath']
52
            ),
53
            new TwigFunction(
54
                'get_article_category_path', [$this, 'getArticleCategoryRouterPath']
55
            ),
56
            new TwigFunction(
57
                'get_article_categories', [$this, 'getCategories']
58
            ),
59
            new TwigFunction(
60
                'get_article_tags', [$this, 'getTags']
61
            ),
62
        ];
63
    }
64
65
    /**
66
     * Get tags array for view.
67
     *
68
     * @param string $className
69
     *
70
     * @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...
71
     */
72 View Code Duplication
    public function getTags(Request $request, $className)
73
    {
74
        $context = [];
75
76
        $tagRepository = $this->em->getRepository($className);
77
        $context['tags'] = $tagRepository->findBy([], ['name' => 'ASC']);
78
79
        $searchTag = $request->get('tag') ? explode(',', $request->get('tag')) : null;
80
        if ($searchTag) {
81
            $context['activeTag'] = true;
82
            $context['activeTags'] = $searchTag;
83
        }
84
85
        return $context;
86
    }
87
88
    /**
89
     * Get categories array for view.
90
     *
91
     * @param string $className
92
     *
93
     * @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...
94
     */
95 View Code Duplication
    public function getCategories(Request $request, $className)
96
    {
97
        $context = [];
98
99
        $categoryRepository = $this->em->getRepository($className);
100
        $context['categories'] = $categoryRepository->findBy([], ['name' => 'ASC']);
101
102
        $searchCategory = $request->get('category') ? explode(',', $request->get('category')) : null;
103
        if ($searchCategory) {
104
            $context['activeCategory'] = true;
105
            $context['activeCategories'] = $searchCategory;
106
        }
107
108
        return $context;
109
    }
110
111
    /**
112
     * @param string $slug
113
     * @param string $tag
114
     * @param string $locale
115
     * @param array  $parameters
116
     * @param int    $referenceType
117
     *
118
     * @return string
119
     */
120
    public function getArticleTagRouterPath($slug, $tag, $locale, $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
121
    {
122
        $routeName = sprintf('_slug_tag_%s', $locale);
123
124
        return $this->getArticleRouterPath($routeName, 'tag', $slug, $tag, $locale, $parameters, $referenceType);
125
    }
126
127
    /**
128
     * @param string $slug
129
     * @param string $category
130
     * @param string $locale
131
     * @param array  $parameters
132
     * @param int    $referenceType
133
     *
134
     * @return string
135
     */
136
    public function getArticleCategoryRouterPath($slug, $category, $locale, $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
137
    {
138
        $routeName = sprintf('_slug_category_%s', $locale);
139
140
        return $this->getArticleRouterPath($routeName, 'category', $slug, $category, $locale, $parameters, $referenceType);
141
    }
142
143
    /**
144
     * @return string
145
     */
146
    public function getName()
147
    {
148
        return 'article_twig_extension';
149
    }
150
151
    /**
152
     * @param string $routeName
153
     * @param string $type
154
     * @param string $slug
155
     * @param string $tagOrCategory
156
     * @param string $locale
157
     * @param array  $parameters
158
     * @param int    $referenceType
159
     *
160
     * @return string
161
     */
162
    protected function getArticleRouterPath($routeName, $type, $slug, $tagOrCategory, $locale, $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
163
    {
164
        if (!$this->articleRouteExists($type, $locale)) {
165
            $routeName = '_slug';
166
        }
167
        if (!isset($parameters[$type])) {
168
            $parameters[$type] = $tagOrCategory;
169
        }
170
        if (!isset($parameters['url'])) {
171
            $parameters['url'] = $slug;
172
        }
173
        if (!isset($parameters['_locale'])) {
174
            $parameters['_locale'] = $locale;
175
        }
176
177
        return $this->router->generate($routeName, $parameters, $referenceType);
178
    }
179
180
    /**
181
     * @param string $type
182
     * @param string $locale
183
     *
184
     * @return bool
185
     */
186
    protected function articleRouteExists($type, $locale)
187
    {
188
        $routeName = sprintf('_slug_%s_%s', $type, $locale);
189
190
        try {
191
            return !\is_null($this->router->getRouteCollection()->get($routeName));
192
        } catch (\Exception $e) {
193
            return false;
194
        }
195
    }
196
}
197