SitemapIndex::render()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 8
c 2
b 0
f 1
dl 0
loc 12
rs 10
cc 2
nc 2
nop 1
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\Renderer\NativeRenderer;
10
use Mfonte\Sitemap\Tags\Sitemap;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Mfonte\Sitemap\Sitemap. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
11
use Mfonte\Sitemap\Tags\Tag;
12
13
class SitemapIndex implements Responsable, Renderable
14
{
15
    /** @var \Mfonte\Sitemap\Tags\Sitemap[] */
16
    protected array $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|Sitemap
29
     */
30
    public function add($tag)
31
    {
32
        if (is_string($tag)) {
33
            $tag = Sitemap::create($tag);
34
        }
35
36
        $this->tags[] = $tag;
37
38
        return $this;
39
    }
40
41
    public function getSitemap(string $url): ?Sitemap
42
    {
43
        return collect($this->tags)->first(function (Tag $tag) use ($url) {
0 ignored issues
show
Bug introduced by
$this->tags of type array 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

43
        return collect(/** @scrutinizer ignore-type */ $this->tags)->first(function (Tag $tag) use ($url) {
Loading history...
44
            return $tag->getType() === 'sitemap' && $tag->url === $url;
45
        });
46
    }
47
48
    public function hasSitemap(string $url): bool
49
    {
50
        return (bool) $this->getSitemap($url);
51
    }
52
53
    /**
54
     * Renders the sitemap index.
55
     * Optionally, you can pass a boolean to use the native renderer instead of the blade template. This is useful if you need to use this package in a non-Laravel project.
56
     *
57
     * @param bool $nativeRenderer - if true, uses the native renderer instead of the blade template (default: false)
58
     *
59
     * @return string
60
     */
61
    public function render(bool $nativeRenderer = false): string
62
    {
63
        $tags = $this->tags;
64
65
        if (! $nativeRenderer) {
66
            return view('sitemap::sitemapIndex/index')
67
                ->with(compact('tags'))
68
                ->render();
69
        } else {
70
            $renderer = NativeRenderer::instance(compact('tags'));
71
72
            return $renderer->render('sitemapIndex');
73
        }
74
    }
75
76
    /**
77
     * Writes the sitemap index to file.
78
     * Optionally, you can pass a boolean to use the native renderer instead of the blade template. This is useful if you need to use this package in a non-Laravel project.
79
     *
80
     * @param string $path
81
     * @param bool $nativeRenderer - if true, uses the native renderer instead of the blade template (default: false)
82
     * @param int $flags - see https://www.php.net/manual/en/function.file-put-contents.php
83
     * @param resource $context - see https://www.php.net/manual/en/function.file-put-contents.php
84
     *
85
     * @return self
86
     */
87
    public function writeToFile(string $path, bool $nativeRenderer = false, int $flags = 0, $context = null) : self
88
    {
89
        file_put_contents($path, $this->render($nativeRenderer), $flags, $context);
90
91
        return $this;
92
    }
93
94
    public function writeToDisk(string $disk, string $path) : self
95
    {
96
        Storage::disk($disk)->put($path, $this->render());
97
98
        return $this;
99
    }
100
101
    /**
102
     * Create an HTTP response that represents the object.
103
     *
104
     * @param  \Illuminate\Http\Request  $request
105
     * @return \Symfony\Component\HttpFoundation\Response
106
     */
107
    public function toResponse($request)
108
    {
109
        return Response::make($this->render(), 200, [
110
            'Content-Type' => 'text/xml',
111
        ]);
112
    }
113
}
114