Test Setup Failed
Push — master ( 6b7182...cea96f )
by Matthew
01:49
created

SitemapBuilder::convertFilesToUrls()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 2
1
<?php
2
3
namespace OwnCloud\SiteMapBuilder;
4
5
use OwnCloud\SiteMapBuilder\Iterator\RestructuredTextFilterIterator;
6
use Refinery29\Sitemap\Component;
7
use Refinery29\Sitemap\Writer;
8
9
/**
10
 * Class SitemapBuilder
11
 * @package OwnCloud\SiteMapBuilder
12
 */
13
class SitemapBuilder
14
{
15
    const BASE_PATH = 'https://doc.owncloud.com/server';
16
    const PRIORITY = 0.8;
17
    const VERSION = '10.0';
18
    const FILEPATH_PREFIX_REGEX = '/.*(?=user_manual|admin_manual|developer_manual)/';
19
20
    /**
21
     * @var string
22
     */
23
    private $basePath;
24
25
    /**
26
     * @var string
27
     */
28
    private $version;
29
30
    /**
31
     * @var float
32
     */
33
    private $priority;
34
35
    /**
36
     * @var string
37
     */
38
    private $changeFrequency;
39
40
    /**
41
     * SitemapBuilder constructor.
42
     * @param string $basePath
43
     * @param string $version
44
     * @param float|string $priority
45
     * @param string $changeFrequency
46
     */
47
    public function __construct(
48
        $basePath = self::BASE_PATH,
49
        $version = self::VERSION,
50
        $priority = self::PRIORITY,
51
        $changeFrequency = Component\Url::CHANGE_FREQUENCY_WEEKLY
52
    ) {
53
        $this->basePath = $basePath;
54
        $this->changeFrequency = $changeFrequency;
55
        $this->version = $version;
56
        $this->priority = $priority;
57
    }
58
59
    /**
60
     * Build a list of URLs from the files of the required filetype retrieved
61
     * @param string $path
62
     * @param string $extension
63
     * @return array
64
     */
65
    public function getSitemapUrlList($path, $extension)
66
    {
67
        // retrieve a filtered list of file names, based on the specified extension.
68
        $iterator = new RestructuredTextFilterIterator(new \RecursiveIteratorIterator(
69
            new \RecursiveDirectoryIterator($path, \FilesystemIterator::CURRENT_AS_PATHNAME)
70
        ), $extension);
71
72
        return $this->convertFilesToUrls($iterator, $extension);
73
    }
74
75
    /**
76
     * Generated a valid sitemap.xml string from the list of files supplied
77
     * @param array $fileList
78
     * @return string
79
     */
80
    public function generateSiteMapXml($fileList)
81
    {
82
        $urlList = [];
83
84
        foreach ($fileList as $file) {
85
            $url = new Component\Url($file);
86
            $urlList[] = $url
87
                ->withLastModified(new \DateTime())
88
                ->withChangeFrequency($this->changeFrequency)
89
                ->withPriority($this->priority)
90
            ;
91
        }
92
93
        $urlSetWriter = new Writer\UrlSetWriter();
94
95
        return $urlSetWriter->write(new Component\UrlSet($urlList));
96
    }
97
98
    /**
99
     * Convert a list of retrieved files to an equivalent list of URLs
100
     *
101
     * @param \Traversable $iterator
102
     * @param string $extension
103
     * @return array
104
     */
105
    private function convertFilesToUrls(\Traversable $iterator, $extension)
106
    {
107
        $urlList = [];
108
109
        foreach ($iterator as $filename) {
110
            $url = str_replace(
111
                ['\\', $extension],
112
                ['/', 'html'],
113
                preg_replace(self::FILEPATH_PREFIX_REGEX, '', $filename)
114
            );
115
            $urlList[] = sprintf('%s/%s/%s', $this->basePath, $this->version, $url);
116
        }
117
118
        return $urlList;
119
    }
120
}
121