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.
Completed
Push — master ( 0acf1f...91750d )
by Christian
71:33
created

SitemapGenerator::fetch()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8977
c 0
b 0
f 0
cc 6
nc 6
nop 1
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\Model\SitemapInterface;
15
use Core23\SitemapBundle\Model\SitemapManagerInterface;
16
use Core23\SitemapBundle\Model\UrlInterface;
17
use Core23\SitemapBundle\Sitemap\SitemapServiceManagerInterface;
18
use Doctrine\Common\Cache\Cache;
19
20
final class SitemapGenerator implements SitemapGeneratorInterface
21
{
22
    /**
23
     * @var Cache|null
24
     */
25
    private $cache;
26
27
    /**
28
     * @var SitemapServiceManagerInterface
29
     */
30
    private $serviceManager;
31
32
    /**
33
     * @var SitemapManagerInterface
34
     */
35
    private $sitemapManager;
36
37
    /**
38
     * @param SitemapServiceManagerInterface $serviceManager
39
     * @param SitemapManagerInterface        $sitemapManager
40
     * @param Cache|null                     $cache
41
     */
42
    public function __construct(SitemapServiceManagerInterface $serviceManager, SitemapManagerInterface $sitemapManager, Cache $cache = null)
43
    {
44
        $this->serviceManager = $serviceManager;
45
        $this->sitemapManager = $sitemapManager;
46
        $this->cache          = $cache;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function toXML(): string
53
    {
54
        $xml = '<?xml version="1.0" encoding="UTF-8"?>';
55
        $xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ';
56
        $xml .= 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ';
57
        $xml .= 'xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';
58
59
        /** @var SitemapInterface $sitemap */
60
        foreach ($this->sitemapManager->getAll() as $sitemap) {
61
            $serviceXml = $this->fetch($sitemap);
62
63
            if ($serviceXml) {
64
                $xml .= $serviceXml;
65
            }
66
        }
67
68
        $xml .= '</urlset>';
69
70
        return $xml;
71
    }
72
73
    /**
74
     * Get eventual cached data or generate whole sitemap.
75
     *
76
     * @param SitemapInterface $sitemap
77
     *
78
     * @return string
79
     */
80
    public function fetch(SitemapInterface $sitemap): string
81
    {
82
        $name = md5(serialize($sitemap));
83
84
        if ($this->cache && $this->cache->contains($name)) {
85
            return $this->cache->fetch($name);
86
        }
87
88
        $service = $this->serviceManager->get($sitemap);
89
90
        if (!$service) {
91
            return '';
92
        }
93
94
        $xml = '';
95
        foreach ($service->execute($sitemap) as $entry) {
96
            $xml .= $this->getLocEntry($entry);
97
        }
98
99
        if ($this->cache) {
100
            $this->cache->save($name, $xml, $sitemap->getTtl());
101
        }
102
103
        return $xml;
104
    }
105
106
    /**
107
     * @param UrlInterface $url
108
     *
109
     * @return string
110
     */
111
    private function getLocEntry(UrlInterface $url): string
112
    {
113
        return '<url>'.
114
        '<loc>'.$url->getLoc().'</loc>'.
115
        (null !== $url->getLastMod() ? '<lastmod>'.$url->getLastMod()->format('c').'</lastmod>' : '').
116
        (null !== $url->getChangeFreq() ? '<changefreq>'.$url->getChangeFreq().'</changefreq>' : '').
117
        (null  !== $url->getPriority() ? '<priority>'.$url->getPriority().'</priority>' : '').
118
        '</url>';
119
    }
120
}
121