Completed
Push — master ( d6e5bd...91fdab )
by Sander
13:05
created

ArticleBundle/Twig/ArticleTwigExtension.php (1 issue)

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
     * ArticleTwigExtension constructor.
29
     *
30
     * @param EntityManagerInterface $em
31
     * @param RouterInterface        $router
32
     */
33
    public function __construct(EntityManagerInterface $em, RouterInterface $router)
34
    {
35
        $this->em = $em;
36
        $this->router = $router;
37
38
        if (func_num_args() > 2) {
39
            @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);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

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