Sitemap   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 68
rs 10
c 0
b 0
f 0
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getUrl() 0 4 2
A create() 0 3 1
A add() 0 9 2
A hasUrl() 0 3 1
A render() 0 10 2
A writeToFile() 0 5 1
1
<?php
2
3
namespace safaeean\Sitemap;
4
5
use safaeean\Sitemap\Tags\Tag;
6
use safaeean\Sitemap\Tags\Url;
7
8
class Sitemap
9
{
10
    /** @var array */
11
    protected $tags = [];
12
13
    /**
14
     * @return static
15
     */
16
    public static function create()
17
    {
18
        return new static();
19
    }
20
21
    /**
22
     * @param string|\safaeean\Sitemap\Tags\Tag $tag
23
     *
24
     * @return $this
25
     */
26
    public function add($tag)
27
    {
28
        if (is_string($tag)) {
29
            $tag = Url::create($tag);
30
        }
31
32
        $this->tags[] = $tag;
33
34
        return $this;
35
    }
36
37
    /**
38
     * @param string $url
39
     *
40
     * @return \safaeean\Sitemap\Tags\Url|null
41
     */
42
    public function getUrl(string $url)
43
    {
44
        return collect($this->tags)->first(function (Tag $tag) use ($url) {
45
            return $tag->getType() === 'url' && $tag->url === $url;
46
        });
47
    }
48
49
    public function hasUrl(string $url): bool
50
    {
51
        return (bool)$this->getUrl($url);
52
    }
53
54
    public function render($sort = false): string
55
    {
56
        if ($sort)
57
            sort($this->tags);
58
59
        $tags = $this->tags;
60
61
        return view('laravel-sitemap::sitemap')
62
            ->with(compact('tags'))
63
            ->render();
64
    }
65
66
    /**
67
     * @param string $path
68
     *
69
     * @return $this
70
     */
71
    public function writeToFile(string $path)
72
    {
73
        file_put_contents($path, $this->render());
74
75
        return $this;
76
    }
77
}
78