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

SitemapIndex::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 0
c 0
b 0
f 0
dl 0
loc 2
rs 10
cc 1
nc 1
nop 0
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\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...
10
use Mfonte\Sitemap\Tags\Tag;
11
12
class SitemapIndex implements Responsable, Renderable
13
{
14
    /** @var \Mfonte\Sitemap\Tags\Sitemap[] */
15
    protected array $tags = [];
16
17
    public static function create()
18
    {
19
        return new static();
20
    }
21
22
    final public function __construct()
23
    {
24
    }
25
26
    /**
27
     * @param $tag string|Sitemap
28
     */
29
    public function add($tag)
30
    {
31
        if (is_string($tag)) {
32
            $tag = Sitemap::create($tag);
33
        }
34
35
        $this->tags[] = $tag;
36
37
        return $this;
38
    }
39
40
    public function getSitemap(string $url): ?Sitemap
41
    {
42
        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

42
        return collect(/** @scrutinizer ignore-type */ $this->tags)->first(function (Tag $tag) use ($url) {
Loading history...
43
            return $tag->getType() === 'sitemap' && $tag->url === $url;
44
        });
45
    }
46
47
    public function hasSitemap(string $url): bool
48
    {
49
        return (bool) $this->getSitemap($url);
50
    }
51
52
    public function render(): string
53
    {
54
        $tags = $this->tags;
55
56
        return view('sitemap::sitemapIndex/index')
57
            ->with(compact('tags'))
58
            ->render();
59
    }
60
61
    public function writeToFile(string $path)
62
    {
63
        file_put_contents($path, $this->render());
64
65
        return $this;
66
    }
67
68
    public function writeToDisk(string $disk, string $path)
69
    {
70
        Storage::disk($disk)->put($path, $this->render());
71
72
        return $this;
73
    }
74
75
    /**
76
     * Create an HTTP response that represents the object.
77
     *
78
     * @param  \Illuminate\Http\Request  $request
79
     * @return \Symfony\Component\HttpFoundation\Response
80
     */
81
    public function toResponse($request)
82
    {
83
        return Response::make($this->render(), 200, [
84
            'Content-Type' => 'text/xml',
85
        ]);
86
    }
87
}
88