GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

SitemapGenerator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * (c) Christian Gripp <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Core23\SitemapBundle\Generator;
13
14
use Core23\SitemapBundle\Definition\DefintionManagerInterface;
15
use Core23\SitemapBundle\Definition\SitemapDefinitionInterface;
16
use Core23\SitemapBundle\Model\UrlInterface;
17
use Core23\SitemapBundle\Sitemap\SitemapServiceManagerInterface;
18
use Psr\SimpleCache\CacheInterface;
19
use Psr\SimpleCache\InvalidArgumentException;
20
use RuntimeException;
21
22
final class SitemapGenerator implements SitemapGeneratorInterface
23
{
24
    /**
25
     * @var CacheInterface|null
26
     */
27
    private $cache;
28
29
    /**
30
     * @var SitemapServiceManagerInterface
31
     */
32
    private $sitemapServiceManager;
33
34
    /**
35
     * @var DefintionManagerInterface
36
     */
37
    private $defintionManager;
38
39
    public function __construct(SitemapServiceManagerInterface $sitemapServiceManager, DefintionManagerInterface $defintionManager, CacheInterface $cache = null)
40
    {
41
        $this->sitemapServiceManager = $sitemapServiceManager;
42
        $this->defintionManager      = $defintionManager;
43
        $this->cache                 = $cache;
44
    }
45
46
    public function toXML(): string
47
    {
48
        $xml = '<?xml version="1.0" encoding="UTF-8"?>';
49
        $xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ';
50
        $xml .= 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ';
51
        $xml .= 'xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';
52
53
        foreach ($this->defintionManager->getAll() as $sitemap) {
54
            try {
55
                $xml .= $this->fetch($sitemap);
56
            } catch (InvalidArgumentException $exception) {
57
                throw new RuntimeException('Error accessing cache', $exception->getCode(), $exception);
58
            }
59
        }
60
61
        $xml .= '</urlset>';
62
63
        return $xml;
64
    }
65
66
    /**
67
     * Get eventual cached data or generate whole sitemap.
68
     *
69
     * @throws InvalidArgumentException
70
     */
71
    private function fetch(SitemapDefinitionInterface $definition): string
72
    {
73
        $name = sprintf('Sitemap_%s', md5(serialize($definition)));
74
75
        if (null !== $this->cache && $this->cache->has($name)) {
76
            return $this->cache->get($name);
77
        }
78
79
        $service = $this->sitemapServiceManager->get($definition);
80
81
        if (null === $service) {
82
            return '';
83
        }
84
85
        $xml = '';
86
        foreach ($service->execute($definition) as $entry) {
87
            $xml .= $this->getLocEntry($entry);
88
        }
89
90
        if (null !== $this->cache) {
91
            $this->cache->set($name, $xml, $definition->getTtl());
92
        }
93
94
        return $xml;
95
    }
96
97
    private function getLocEntry(UrlInterface $url): string
98
    {
99
        return '<url>'.
100
        '<loc>'.$url->getLoc().'</loc>'.
101
        (null !== $url->getLastMod() ? '<lastmod>'.$url->getLastMod()->format('c').'</lastmod>' : '').
102
        (null !== $url->getChangeFreq() ? '<changefreq>'.$url->getChangeFreq().'</changefreq>' : '').
103
        (null  !== $url->getPriority() ? '<priority>'.$url->getPriority().'</priority>' : '').
104
        '</url>';
105
    }
106
}
107