Completed
Push — master ( 71b92c...603b5c )
by ARCANEDEV
11s
created

SitemapManager::saveMultiple()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
ccs 8
cts 8
cp 1
crap 2
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
use Illuminate\Support\Str;
8
9
/**
10
 * Class     SitemapManager
11
 *
12
 * @package  Arcanedev\LaravelSitemap
13
 * @author   ARCANEDEV <[email protected]>
14
 */
15
class SitemapManager implements SitemapManagerContract
16
{
17
    /* -----------------------------------------------------------------
18
     |  Properties
19
     | -----------------------------------------------------------------
20
     */
21
22
    /** @var  \Illuminate\Support\Collection */
23
    protected $sitemaps;
24
25
    /** @var  string */
26
    protected $format = 'xml';
27
28
    /* -----------------------------------------------------------------
29
     |  Constructor
30
     | -----------------------------------------------------------------
31
     */
32
33
    /**
34
     * SitemapManager constructor.
35
     */
36 76
    public function __construct()
37
    {
38 76
        $this->sitemaps = new Collection;
39 76
    }
40
41
    /* -----------------------------------------------------------------
42
     |  Getters & Setters
43
     | -----------------------------------------------------------------
44
     */
45
46
    /**
47
     * Set the format.
48
     *
49
     * @param  string  $format
50
     *
51
     * @return self
52
     */
53 20
    public function format($format)
54
    {
55 20
        $this->format = $format;
56
57 20
        return $this;
58
    }
59
60
    /* -----------------------------------------------------------------
61
     |  Main Methods
62
     | -----------------------------------------------------------------
63
     */
64
65
    /**
66
     * Create and add a sitemap to the collection.
67
     *
68
     * @param  string    $name
69
     * @param  callable  $callback
70
     *
71
     * @return self
72
     */
73 4
    public function create($name, callable $callback)
74
    {
75 4
        return $this->add($name, tap(Sitemap::make()->setPath($name), $callback));
76
    }
77
78
    /**
79
     * Add a sitemap to the collection.
80
     *
81
     * @param  string                                                $name
82
     * @param  \Arcanedev\LaravelSitemap\Contracts\Entities\Sitemap  $sitemap
83
     *
84
     * @return self
85
     */
86 64
    public function add($name, SitemapContract $sitemap)
87
    {
88 64
        $this->sitemaps->put($name, $sitemap);
89
90 64
        return $this;
91
    }
92
93
    /**
94
     * Get the sitemaps collection.
95
     *
96
     * @return \Illuminate\Support\Collection
97
     */
98 20
    public function all()
99
    {
100 20
        return $this->sitemaps;
101
    }
102
103
    /**
104
     * Get a sitemap instance.
105
     *
106
     * @param  string      $name
107
     * @param  mixed|null  $default
108
     *
109
     * @return mixed
110
     */
111 8
    public function get($name, $default = null)
112
    {
113 8
        return $this->sitemaps->get($name, $default);
114
    }
115
116
    /**
117
     * Check if a sitemap exists.
118
     *
119
     * @param  string  $name
120
     *
121
     * @return bool
122
     */
123 8
    public function has($name)
124
    {
125 8
        if ( ! Str::contains($name, '.'))
126 4
            return $this->sitemaps->has($name);
127
128 4
        list($name, $key) = explode('.', $name, 2);
129
130
        $map = $this->sitemaps->filter(function (SitemapContract $map) {
131 4
            return $map->isExceeded();
132 4
        })->get($name);
133
134 4
        return is_null($map)
135
            ? false
136 4
            : $map->chunk()->has(intval($key));
137
    }
138
139
    /**
140
     * Remove a sitemap from the collection by key.
141
     *
142
     * @param  string|array  $names
143
     *
144
     * @return self
145
     */
146 4
    public function forget($names)
147
    {
148 4
        $this->sitemaps->forget($names);
149
150 4
        return $this;
151
    }
152
153
    /**
154
     * Get the sitemaps count.
155
     *
156
     * @return int
157
     */
158 16
    public function count()
159
    {
160 16
        return $this->sitemaps->count();
161
    }
162
163
    /**
164
     * Render the sitemaps.
165
     *
166
     * @param  string  $name
167
     *
168
     * @throws \Throwable
169
     *
170
     * @return string|null
171
     */
172 44
    public function render($name = null)
173
    {
174 44
        return SitemapBuilder::make()->build($name, $this->sitemaps, $this->format);
175
    }
176
177
    /**
178
     * Save the sitemaps.
179
     *
180
     * @param  string       $path
181
     * @param  string|null  $name
182
     *
183
     * @throws \Throwable
184
     *
185
     * @return self
186
     */
187 16
    public function save($path, $name = null)
188
    {
189 16
        if ($this->sitemaps->isEmpty())
190 4
            return $this;
191
192 16
        file_put_contents($path, $this->render($name));
193
194
        /** @var  \Arcanedev\LaravelSitemap\Contracts\Entities\Sitemap  $sitemap */
195 16
        foreach ($this->sitemaps as $key => $sitemap) {
196 16
            if ($sitemap->isExceeded())
197 7
                $this->saveMultiple($path, $sitemap);
198
        }
199
200 16
        return $this;
201
    }
202
203
    /**
204
     * Render the Http response.
205
     *
206
     * @param  string  $name
207
     * @param  int     $status
208
     * @param  array   $headers
209
     *
210
     * @throws \Throwable
211
     *
212
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
213
     */
214 4
    public function respond($name = null, $status = 200, array $headers = [])
215
    {
216 4
        return response($this->render($name), $status, array_merge($this->getResponseHeaders(), $headers));
217
    }
218
219
    /**
220
     * Get the collection of items as a plain array.
221
     *
222
     * @return array
223
     */
224 8
    public function toArray()
225
    {
226 8
        return $this->all()->toArray();
227
    }
228
229
    /**
230
     * Get the collection of sitemaps as JSON.
231
     *
232
     * @param  int  $options
233
     *
234
     * @return string
235
     */
236 4
    public function toJson($options = 0)
237
    {
238 4
        return json_encode($this->jsonSerialize(), $options);
239
    }
240
241
    /**
242
     * Convert the object into something JSON serializable.
243
     *
244
     * @return array
245
     */
246 4
    public function jsonSerialize()
247
    {
248 4
        return $this->toArray();
249
    }
250
251
    /**
252
     * Save multiple sitemap.
253
     *
254
     * @param  string                                                $path
255
     * @param  \Arcanedev\LaravelSitemap\Contracts\Entities\Sitemap  $sitemap
256
     *
257
     * @throws \Throwable
258
     */
259 4
    private function saveMultiple($path, $sitemap)
260
    {
261 4
        $pathInfo = pathinfo($path);
262 4
        $chunks   = $sitemap->chunk();
263
264 4
        foreach ($chunks as $key => $item) {
265 4
            file_put_contents(
266 4
                $pathInfo['dirname'].DS.$pathInfo['filename'].'-'.$key.'.'.$pathInfo['extension'],
267 4
                SitemapBuilder::make()->build($key, $chunks, $this->format)
268
            );
269
        }
270 4
    }
271
272
    /**
273
     * Get the response header.
274
     *
275
     * @return array
276
     */
277 4
    protected function getResponseHeaders()
278
    {
279 4
        return array_get([
280 4
            'xml' => ['Content-Type' => 'application/xml'],
281
            'rss' => ['Content-Type' => 'application/rss+xml'],
282
            'txt' => ['Content-Type' => 'text/plain'],
283 4
        ], $this->format, []);
284
    }
285
}
286