Sitemap::isExceeded()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arcanedev\LaravelSitemap\Entities;
6
7
use Arcanedev\LaravelSitemap\Contracts\Entities\Sitemap as SitemapContract;
8
use Illuminate\Support\Collection;
9
use Arcanedev\LaravelSitemap\Contracts\Entities\Url as UrlContract;
10
11
/**
12
 * Class     Sitemap
13
 *
14
 * @package  Arcanedev\LaravelSitemap\Entities
15
 * @author   ARCANEDEV <[email protected]>
16
 */
17
class Sitemap implements SitemapContract
18
{
19
    /* -----------------------------------------------------------------
20
     |  Properties
21
     | -----------------------------------------------------------------
22
     */
23
24
    /** @var string|null */
25
    protected $path;
26
27
    /** @var  \Illuminate\Support\Collection */
28
    protected $urls;
29
30
    /* -----------------------------------------------------------------
31
     |  Constructor
32
     | -----------------------------------------------------------------
33
     */
34
35
    /**
36
     * Sitemap constructor.
37
     */
38 168
    public function __construct()
39
    {
40 168
        $this->urls = new Collection;
41 168
    }
42
43
    /* -----------------------------------------------------------------
44
     |  Getters & Setters
45
     | -----------------------------------------------------------------
46
     */
47
48
    /**
49
     * Set the sitemap path.
50
     *
51
     * @param  string  $path
52
     *
53
     * @return $this
54
     */
55 96
    public function setPath(string $path)
56
    {
57 96
        $this->path = $path;
58
59 96
        return $this;
60
    }
61
62
    /**
63
     * Get the sitemap path.
64
     *
65
     * @return string|null
66
     */
67 42
    public function getPath(): ?string
68
    {
69 42
        return $this->path;
70
    }
71
72
    /**
73
     * Get the sitemap's URLs.
74
     *
75
     * @return \Illuminate\Support\Collection
76
     */
77 108
    public function getUrls(): Collection
78
    {
79 108
        return $this->urls;
80
    }
81
82
    /**
83
     * Set the URLs Collection.
84
     *
85
     * @param  \Illuminate\Support\Collection  $urls
86
     *
87
     * @return $this
88
     */
89 18
    public function setUrls(Collection $urls)
90
    {
91 18
        $this->urls = $urls;
92
93 18
        return $this;
94
    }
95
96
    /* -----------------------------------------------------------------
97
     |  Main Methods
98
     | -----------------------------------------------------------------
99
     */
100
101
    /**
102
     * Make a sitemap instance.
103
     *
104
     * @return $this
105
     */
106 12
    public static function make()
107
    {
108 12
        return new static();
109
    }
110
111
    /**
112
     * Get a URL instance by its loc.
113
     *
114
     * @param  string      $loc
115
     * @param  mixed|null  $default
116
     *
117
     * @return \Arcanedev\LaravelSitemap\Entities\Url|null
118
     */
119 6
    public function getUrl(string $loc, $default = null)
120
    {
121 6
        return $this->getUrls()->get($loc, $default);
122
    }
123
124
    /**
125
     * Add a sitemap URL to the collection.
126
     *
127
     * @param  \Arcanedev\LaravelSitemap\Contracts\Entities\Url  $url
128
     *
129
     * @return $this
130
     */
131 144
    public function add(UrlContract $url)
132
    {
133 144
        $this->urls->put($url->getLoc(), $url);
134
135 144
        return $this;
136
    }
137
138
    /**
139
     * Add many urls to the collection.
140
     *
141
     * @param  iterable|mixed  $urls
142
     *
143
     * @return $this
144
     */
145 6
    public function addMany(iterable $urls)
146
    {
147 6
        foreach ($urls as $url) {
148 6
            $this->add($url);
149
        }
150
151 6
        return $this;
152
    }
153
154
    /**
155
     * Create and Add a sitemap URL to the collection.
156
     *
157
     * @param  string    $loc
158
     * @param  callable  $callback
159
     *
160
     * @return $this
161
     */
162 90
    public function create(string $loc, callable $callback)
163
    {
164 90
        return $this->add(tap(Url::make($loc), $callback));
165
    }
166
167
    /**
168
     * Check if the url exists in the sitemap items.
169
     *
170
     * @param  string  $url
171
     *
172
     * @return bool
173
     */
174 12
    public function has(string $url): bool
175
    {
176 12
        return $this->urls->has($url);
177
    }
178
179
    /**
180
     * Get the urls' count.
181
     *
182
     * @return int
183
     */
184 108
    public function count(): int
185
    {
186 108
        return $this->urls->count();
187
    }
188
189
    /**
190
     * Get the collection of items as a plain array.
191
     *
192
     * @return array
193
     */
194 36
    public function toArray(): array
195
    {
196 36
        return $this->getUrls()->values()->toArray();
197
    }
198
199
    /**
200
     * Get the sitemap and its urls as JSON.
201
     *
202
     * @param  int  $options
203
     *
204
     * @return string
205
     */
206 6
    public function toJson($options = 0): string
207
    {
208 6
        return json_encode($this->jsonSerialize(), $options);
209
    }
210
211
    /**
212
     * Convert the object into something JSON serializable.
213
     *
214
     * @return array
215
     */
216 6
    public function jsonSerialize(): array
217
    {
218 6
        return $this->toArray();
219
    }
220
221
    /**
222
     * Check if the number of URLs is exceeded.
223
     *
224
     * @return bool
225
     */
226 66
    public function isExceeded(): bool
227
    {
228 66
        return $this->count() > $this->getMaxSize();
229
    }
230
231
    /**
232
     * Chunk the sitemap to multiple chunks if the size is exceeded.
233
     *
234
     * @return \Illuminate\Support\Collection
235
     */
236 12
    public function chunk(): Collection
237
    {
238 12
        return $this->urls
239 12
            ->chunk($this->getMaxSize())
240
            ->mapWithKeys(function ($item, $index) {
241 12
                $pathInfo = pathinfo($this->getPath());
242 12
                $index    = $index + 1;
243 12
                $path     = $pathInfo['dirname'].'/'.$pathInfo['filename'].'-'.$index.'.'.$pathInfo['extension'];
244
245
                return [
246 12
                    $index => (new Sitemap)->setPath($path)->setUrls($item),
247
                ];
248 12
            });
249
    }
250
251
    /* -----------------------------------------------------------------
252
     |  Other Methods
253
     | -----------------------------------------------------------------
254
     */
255
256
    /**
257
     * Get the max size.
258
     *
259
     * @return int
260
     */
261 66
    protected function getMaxSize(): int
262
    {
263 66
        return (int) config('sitemap.urls-max-size', 50000);
264
    }
265
}
266