Passed
Branch master (7a2a88)
by Maurizio
14:27
created

Sitemap   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 2
Metric Value
wmc 21
eloc 34
c 3
b 1
f 2
dl 0
loc 108
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 3 1
A writeToDisk() 0 5 1
B add() 0 23 7
A writeToFile() 0 5 1
A __construct() 0 2 1
A hasUrl() 0 3 1
A render() 0 9 1
A toResponse() 0 4 1
A hasNews() 0 4 2
A getUrl() 0 4 2
A getTags() 0 3 1
A hasImages() 0 4 2
1
<?php
2
3
namespace Mfonte\Sitemap;
4
5
use Illuminate\Contracts\Support\Renderable;
6
use Illuminate\Contracts\Support\Responsable;
7
use Illuminate\Support\Facades\Response;
8
use Illuminate\Support\Facades\Storage;
9
use Mfonte\Sitemap\Contracts\Sitemapable;
10
use Mfonte\Sitemap\Tags\Tag;
11
use Mfonte\Sitemap\Tags\Url;
12
13
class Sitemap implements Responsable, Renderable
14
{
15
    /** @var \Mfonte\Sitemap\Tags\Url[] */
16
    protected $tags = [];
17
18
    public static function create()
19
    {
20
        return new static();
21
    }
22
23
    final public function __construct()
24
    {
25
    }
26
27
    /**
28
     * @param $tag string|Url|Sitemapable
29
     */
30
    public function add($tag)
31
    {
32
        if (is_object($tag) && array_key_exists(Sitemapable::class, class_implements($tag))) {
33
            $tag = $tag->toSitemapTag();
34
        }
35
36
        if (is_iterable($tag)) {
37
            foreach ($tag as $item) {
38
                $this->add($item);
39
            }
40
41
            return $this;
42
        }
43
44
        if (is_string($tag)) {
45
            $tag = Url::create($tag);
46
        }
47
48
        if (! in_array($tag, $this->tags)) {
49
            $this->tags[] = $tag;
50
        }
51
52
        return $this;
53
    }
54
55
    public function getTags(): array
56
    {
57
        return $this->tags;
58
    }
59
60
    public function getUrl(string $url): ?Url
61
    {
62
        return collect($this->tags)->first(function (Tag $tag) use ($url) {
0 ignored issues
show
Bug introduced by
$this->tags of type Mfonte\Sitemap\Tags\Url[] is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
        return collect(/** @scrutinizer ignore-type */ $this->tags)->first(function (Tag $tag) use ($url) {
Loading history...
63
            return $tag->getType() === 'url' && $tag->url === $url;
64
        });
65
    }
66
67
    public function hasUrl(string $url): bool
68
    {
69
        return (bool) $this->getUrl($url);
70
    }
71
72
    public function hasImages() : bool
73
    {
74
        return (bool) collect($this->tags)->first(function (Tag $tag) {
0 ignored issues
show
Bug introduced by
$this->tags of type Mfonte\Sitemap\Tags\Url[] is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
        return (bool) collect(/** @scrutinizer ignore-type */ $this->tags)->first(function (Tag $tag) {
Loading history...
75
            return $tag->getType() === 'url' && ! empty($tag->images);
76
        });
77
    }
78
79
    public function hasNews() : bool
80
    {
81
        return (bool) collect($this->tags)->first(function (Tag $tag) {
0 ignored issues
show
Bug introduced by
$this->tags of type Mfonte\Sitemap\Tags\Url[] is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

81
        return (bool) collect(/** @scrutinizer ignore-type */ $this->tags)->first(function (Tag $tag) {
Loading history...
82
            return $tag->getType() === 'url' && ! empty($tag->news);
83
        });
84
    }
85
86
    public function render(): string
87
    {
88
        $tags = collect($this->tags)->unique('url')->filter();
0 ignored issues
show
Bug introduced by
$this->tags of type Mfonte\Sitemap\Tags\Url[] is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

88
        $tags = collect(/** @scrutinizer ignore-type */ $this->tags)->unique('url')->filter();
Loading history...
89
        $hasImages = $this->hasImages();
90
        $hasNews = $this->hasNews();
91
92
        return view('sitemap::sitemap')
93
            ->with(compact('tags', 'hasImages', 'hasNews'))
94
            ->render();
95
    }
96
97
    public function writeToFile(string $path)
98
    {
99
        file_put_contents($path, $this->render());
100
101
        return $this;
102
    }
103
104
    public function writeToDisk(string $disk, string $path)
105
    {
106
        Storage::disk($disk)->put($path, $this->render());
107
108
        return $this;
109
    }
110
111
    /**
112
     * Create an HTTP response that represents the object.
113
     *
114
     * @param  \Illuminate\Http\Request  $request
115
     * @return \Symfony\Component\HttpFoundation\Response
116
     */
117
    public function toResponse($request)
118
    {
119
        return Response::make($this->render(), 200, [
120
            'Content-Type' => 'text/xml',
121
        ]);
122
    }
123
}
124