Completed
Pull Request — master (#9)
by ARCANEDEV
13:07
created

SitemapManager::save()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
1
<?php namespace Arcanedev\LaravelSitemap;
2
3
use Arcanedev\LaravelSitemap\Contracts\Entities\Sitemap as SitemapContract;
4
use Arcanedev\LaravelSitemap\Contracts\SitemapManager as SitemapManagerContract;
5
use Arcanedev\LaravelSitemap\Entities\Sitemap;
6
use Illuminate\Support\Collection;
7
8
/**
9
 * Class     SitemapManager
10
 *
11
 * @package  Arcanedev\LaravelSitemap
12
 * @author   ARCANEDEV <[email protected]>
13
 */
14
class SitemapManager implements SitemapManagerContract
15
{
16
    /* -----------------------------------------------------------------
17
     |  Properties
18
     | -----------------------------------------------------------------
19
     */
20
21
    /** @var  \Illuminate\Support\Collection */
22
    protected $sitemaps;
23
24
    /** @var  string */
25
    protected $format = 'xml';
26
27
    /* -----------------------------------------------------------------
28
     |  Constructor
29
     | -----------------------------------------------------------------
30
     */
31
32
    /**
33
     * SitemapManager constructor.
34
     */
35 34
    public function __construct()
36
    {
37 34
        $this->sitemaps = new Collection;
38 34
    }
39
40
    /* -----------------------------------------------------------------
41
     |  Getters & Setters
42
     | -----------------------------------------------------------------
43
     */
44
45
    /**
46
     * Set the format.
47
     *
48
     * @param  string  $format
49
     *
50
     * @return self
51
     */
52 8
    public function format($format)
53
    {
54 8
        $this->format = $format;
55
56 8
        return $this;
57
    }
58
59
    /* -----------------------------------------------------------------
60
     |  Main Methods
61
     | -----------------------------------------------------------------
62
     */
63
64
    /**
65
     * Create and add a sitemap to the collection.
66
     *
67
     * @param  string    $name
68
     * @param  callable  $callback
69
     *
70
     * @return self
71
     */
72 2
    public function create($name, callable $callback)
73
    {
74 2
        return $this->add($name, tap((new Sitemap)->setPath($name), $callback));
75
    }
76
77
    /**
78
     * Add a sitemap to the collection.
79
     *
80
     * @param  string                                                $name
81
     * @param  \Arcanedev\LaravelSitemap\Contracts\Entities\Sitemap  $sitemap
82
     *
83
     * @return self
84
     */
85 28
    public function add($name, SitemapContract $sitemap)
86
    {
87 28
        $this->sitemaps->put($name, $sitemap);
88
89 28
        return $this;
90
    }
91
92
    /**
93
     * Get the sitemaps collection.
94
     *
95
     * @return \Illuminate\Support\Collection
96
     */
97 10
    public function all()
98
    {
99 10
        return $this->sitemaps;
100
    }
101
102
    /**
103
     * Get a sitemap instance.
104
     *
105
     * @param  string      $name
106
     * @param  mixed|null  $default
107
     *
108
     * @return mixed
109
     */
110 4
    public function get($name, $default = null)
111
    {
112 4
        return $this->sitemaps->get($name, $default);
113
    }
114
115
    /**
116
     * Check if a sitemap exists.
117
     *
118
     * @param  string  $name
119
     *
120
     * @return bool
121
     */
122 2
    public function has($name)
123
    {
124 2
        return $this->sitemaps->has($name);
125
    }
126
127
    /**
128
     * Remove a sitemap from the collection by key.
129
     *
130
     * @param  string|array  $names
131
     *
132
     * @return self
133
     */
134 2
    public function forget($names)
135
    {
136 2
        $this->sitemaps->forget($names);
137
138 2
        return $this;
139
    }
140
141
    /**
142
     * Get the sitemaps count.
143
     *
144
     * @return int
145
     */
146 8
    public function count()
147
    {
148 8
        return $this->sitemaps->count();
149
    }
150
151
    /**
152
     * Render the sitemaps.
153
     *
154
     * @param  string  $name
155
     *
156
     * @return string|null
157
     */
158 18
    public function render($name = null)
159
    {
160 18
        return SitemapBuilder::make()->build($name, $this->sitemaps, $this->format);
161
    }
162
163
    /**
164
     * Save the sitemaps.
165
     *
166
     * @param  string       $path
167
     * @param  string|null  $name
168
     *
169
     * @return self
170
     */
171 6
    public function save($path, $name = null)
172
    {
173 6
        if ($this->sitemaps->isNotEmpty()) {
174 6
            file_put_contents($path, $this->render($name));
175
        }
176
177 6
        return $this;
178
    }
179
180
    /**
181
     * Get the collection of items as a plain array.
182
     *
183
     * @return array
184
     */
185 4
    public function toArray()
186
    {
187 4
        return $this->all()->toArray();
188
    }
189
190
    /**
191
     * Get the collection of sitemaps as JSON.
192
     *
193
     * @param  int  $options
194
     *
195
     * @return string
196
     */
197 2
    public function toJson($options = 0)
198
    {
199 2
        return json_encode($this->jsonSerialize(), $options);
200
    }
201
202
    /**
203
     * Convert the object into something JSON serializable.
204
     *
205
     * @return array
206
     */
207 2
    public function jsonSerialize()
208
    {
209 2
        return $this->toArray();
210
    }
211
}
212