Completed
Pull Request — master (#2743)
by Jeroen
16:52 queued 10:35
created

SitemapController::dispatch()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 11
loc 11
ccs 0
cts 9
cp 0
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 6
1
<?php
2
3
namespace Kunstmaan\SitemapBundle\Controller;
4
5
use Kunstmaan\SitemapBundle\Event\PreSitemapRenderEvent;
6
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
7
use Symfony\Component\Routing\Annotation\Route;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
9
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
10
use Symfony\Component\HttpFoundation\Request;
11
12
class SitemapController extends Controller
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...e\Controller\Controller has been deprecated with message: since Symfony 4.2, use "Symfony\Bundle\FrameworkBundle\Controller\AbstractController" 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...
13
{
14
    /**
15
     * This will generate a sitemap for the specified locale.
16
     * Use the mode parameter to select in which mode the sitemap should be
17
     * generated. At this moment only XML is supported
18
     *
19
     * @Route("/sitemap-{locale}.{_format}", name="KunstmaanSitemapBundle_sitemap",
20
     *                                       requirements={"_format" = "xml"})
21
     * @Template("@KunstmaanSitemap/Sitemap/view.xml.twig")
22
     *
23
     * @param $locale
24
     *
25
     * @return array
26
     */
27
    public function sitemapAction($locale)
28
    {
29
        $nodeMenu = $this->get('kunstmaan_node.node_menu');
30
        $nodeMenu->setLocale($locale);
31
        $nodeMenu->setIncludeOffline(false);
32
        $nodeMenu->setIncludeHiddenFromNav(true);
33
        $nodeMenu->setCurrentNode(null);
34
35
        $event = new PreSitemapRenderEvent($locale);
36
        $this->dispatch($event, PreSitemapRenderEvent::NAME);
37
38
        return array(
39
            'nodemenu' => $nodeMenu,
40
            'locale' => $locale,
41
            'extraItems' => $event->getExtraItems(),
42
        );
43
    }
44
45
    /**
46
     * This will generate a sitemap index file to define a sub sitemap for each
47
     * language. Info at:
48
     * https://support.google.com/webmasters/answer/75712?rd=1 Use the mode
49
     * parameter to select in which mode the sitemap should be generated. At
50
     * this moment only XML is supported
51
     *
52
     * @Route("/sitemap.{_format}", name="KunstmaanSitemapBundle_sitemapindex",
53
     *                              requirements={"_format" = "xml"})
54
     * @Template("@KunstmaanSitemap/SitemapIndex/view.xml.twig")
55
     *
56
     * @param Request $request
57
     *
58
     * @return array
59
     */
60
    public function sitemapIndexAction(Request $request)
61
    {
62
        $locales = $this->get('kunstmaan_admin.domain_configuration')
63
            ->getBackendLocales();
64
65
        return array(
66
            'locales' => $locales,
67
            'host' => $request->getSchemeAndHttpHost(),
68
        );
69
    }
70
71
    /**
72
     * @param object $event
73
     * @param string $eventName
74
     *
75
     * @return object
76
     */
77 View Code Duplication
    private function dispatch($event, string $eventName)
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...
78
    {
79
        $eventDispatcher = $this->container->get('event_dispatcher');
80
        if (class_exists(LegacyEventDispatcherProxy::class)) {
81
            $eventDispatcher = LegacyEventDispatcherProxy::decorate($eventDispatcher);
82
83
            return $eventDispatcher->dispatch($event, $eventName);
84
        }
85
86
        return $eventDispatcher->dispatch($eventName, $event);
87
    }
88
}
89