Sitemap::getExposedRoutes()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 3
nop 0
dl 0
loc 14
ccs 7
cts 7
cp 1
crap 4
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace Norsys\SeoBundle\Action;
5
6
use Symfony\Component\Routing\RouterInterface;
7
use Symfony\Component\HttpFoundation\Response;
8
use Symfony\Component\Templating\EngineInterface;
9
10
/**
11
 * Class Sitemap
12
 * Responsible for displaying the sitemap XML file
13
 * To be exposed in the sitemap, a route must have the option
14
 * "sitemap" set to true in the routing config.
15
 *
16
 * Example:
17
 *
18
 * #app/config/routing.yml
19
 * home:
20
 *     path: /
21
 *     defaults: { _controller: AppBundle\Action\Home }
22
 *     options:
23
 *         sitemap: true
24
 */
25
class Sitemap
26
{
27
    /**
28
     * @var RouterInterface
29
     */
30
    private $router;
31
32
    /**
33
     * @var EngineInterface
34
     */
35
    private $templateEngine;
36
37
    /**
38
     * Sitemap constructor.
39
     *
40
     * @param RouterInterface $router
41
     * @param EngineInterface $templateEngine
42
     */
43
    public function __construct(RouterInterface $router, EngineInterface $templateEngine)
44
    {
45 1
        $this->router = $router;
46 1
        $this->templateEngine = $templateEngine;
47 1
    }
48
49
    /**
50
     * @return Response
51
     */
52
    public function __invoke() : Response
53
    {
54 1
        return new Response(
55 1
            $this->templateEngine->render(
56 1
                'NorsysSeoBundle::sitemap.xml.twig',
57
                [
58 1
                    'routes' => $this->getExposedRoutes()
59
                ]
60
            ),
61 1
            Response::HTTP_OK,
62
            [
63 1
                'Content-Type' => 'text/xml',
64
            ]
65
        );
66
    }
67
68
    /**
69
     * Return a list of exposed route names
70
     *
71
     * @return array
72
     */
73
    private function getExposedRoutes() : array
74
    {
75 1
        $routes = [];
76 1
        $routeCollection = $this->router->getRouteCollection();
77
78 1
        foreach ($routeCollection as $name => $route) {
79 1
            if ($route->hasOption('sitemap') === true
80 1
                && $route->getOption('sitemap') === true
81
            ) {
82 1
                $routes[] = $name;
83
            }
84
        }
85
86 1
        return $routes;
87
    }
88
}
89