Completed
Push — master ( e4af4c...885e6f )
by D.
02:12
created

SiteMapGenerator::addSiteMapUrls()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
rs 9.6666
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
/**
3
 * This file is part of sitemap-common.
4
 *
5
 * (c) 2016 Daniele Moraschi
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace SiteMap;
12
13
14
use SiteMap\Schema\SiteMapUrl;
15
use SiteMap\Schema\SiteMapUrlCollection;
16
use SiteMap\Http\Url;
17
use SiteMap\Output\Writer;
18
use SiteMap\Template\Template;
19
20
/**
21
 * Class SiteMapGenerator
22
 * @package SiteMap
23
 */
24
class SiteMapGenerator
25
{
26
27
    /**
28
     * @var Writer
29
     */
30
    private $writer;
31
32
    /**
33
     * @var Template
34
     */
35
    private $template;
36
37
    /**
38
     * @var SiteMapUrlCollection
39
     */
40
    private $collection;
41
42
    /**
43
     * SiteMapGenerator constructor.
44
     *
45
     * @param Writer $writer
46
     * @param Template $template
47
     */
48
    public function __construct(Writer $writer, Template $template)
49
    {
50
        $this->writer = $writer;
51
        $this->template = $template;
52
        $this->collection = new SiteMapUrlCollection();
53
    }
54
55
    /**
56
     * Set the SiteMapUrlCollection for the sitemap.
57
     *
58
     * @param SiteMapUrlCollection $siteMapUrlCollection
59
     * @return SiteMapUrlCollection
60
     */
61
    public function setCollection(SiteMapUrlCollection $siteMapUrlCollection)
62
    {
63
        return $this->collection = $siteMapUrlCollection;
64
    }
65
66
    /**
67
     * Add a SiteMapUrl to the sitemap.
68
     *
69
     * @param SiteMapUrl $siteMapUrl
70
     * @return SiteMapUrl
71
     */
72
    public function addSiteMapUrl(SiteMapUrl $siteMapUrl)
73
    {
74
        return $this->collection->add($siteMapUrl);
75
    }
76
77
    /**
78
     * Add a array of SiteMapUrl to the sitemap.
79
     *
80
     * @param array $urls
81
     * @return $this
82
     */
83
    public function addSiteMapUrls(array $urls = array())
84
    {
85
        /** @var SiteMapUrl $siteMapUrl */
86
        foreach ($urls as $siteMapUrl) {
87
            $this->addSiteMapUrl($siteMapUrl);
88
        }
89
90
        return $this;
91
    }
92
93
    /**
94
     * Add URL to the sitemap
95
     *
96
     * @param mixed string|Url $url
97
     * @param $frequency
98
     * @param $priority
99
     * @return SiteMapUrl
100
     */
101
    public function addUrl($url, $frequency = SiteMapUrl::DAILY, $priority = 0.3)
102
    {
103
        if (! $url instanceof Url) {
104
            $url = new Url((string) $url);
105
        }
106
107
        $siteMapUrl = new SiteMapUrl(
108
            $url, $frequency, $priority
109
        );
110
        
111
        return $this->addSiteMapUrl($siteMapUrl);
112
    }
113
114
    /**
115
     * Generate the sitemap.
116
     *
117
     * @return mixed
118
     */
119
    public function execute()
120
    {
121
        $content = $this->template->map($this->collection);
122
        return $this->writer->write($content);
123
    }
124
125
}