SitemapListener::isAsset()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace CliveWalkden\JigsawSitemap;
4
5
use Illuminate\Support\Str;
6
use samdark\sitemap\Sitemap;
7
use TightenCo\Jigsaw\Jigsaw;
8
9
class SitemapListener
10
{
11
    /**
12
     * Jigsaw instance.
13
     */
14
    protected $jigsaw;
15
16
    public function handle(Jigsaw $jigsaw)
17
    {
18
        $this->jigsaw = $jigsaw;
19
20
        $baseUrl = $jigsaw->getConfig('baseUrl');
21
        if (empty($baseUrl)) {
22
            echo "\nTo generate a sitemap.xml file, please specify a 'baseUrl' in config.php.\n\n";
23
24
            return;
25
        }
26
27
        $this->generateSitemap($baseUrl);
28
        if ($jigsaw->getConfig('sitemap.image_sitemap.generate')) {
29
            $this->generateImageSitemap($baseUrl);
30
        }
31
    }
32
33
    private function generateSitemap($baseUrl)
34
    {
35
        $sitemap = new Sitemap($this->jigsaw->getDestinationPath().'/sitemap.xml');
36
37
        collect($this->jigsaw->getOutputPaths())->each(function ($path) use ($baseUrl, $sitemap) {
38
            // Don't index assets
39
            if (!$this->isAsset($path) && !$this->isExcluded($path)) {
40
                // Check if a file extension is present
41
                if ($ext = pathinfo($path, PATHINFO_EXTENSION)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $ext is dead and can be removed.
Loading history...
42
                    $sitemap->addItem(rtrim($baseUrl, '/').$path, time(), Sitemap::MONTHLY);
43
                } else {
44
                    $sitemap->addItem(rtrim($baseUrl, '/').$path.($this->jigsaw->getConfig('sitemap.url_trailing_slash') ? '/' : ''), time(), Sitemap::MONTHLY);
45
                }
46
            }
47
        });
48
49
        $sitemap->write();
50
    }
51
52
    private function generateImageSitemap($baseUrl)
53
    {
54
        $sitemap = new Sitemap($this->jigsaw->getDestinationPath().'/'.($this->jigsaw->getConfig('sitemap.image_sitemap.filename') ?: 'sitemap_images.xml'));
55
        $exts = $this->jigsaw->getConfig('sitemap.image_sitemap.extensions');
56
        $supported_extensions = $exts ? $exts->toArray() : [];
57
58
        collect($this->jigsaw->getOutputPaths())->each(function ($path) use ($baseUrl, $sitemap, $supported_extensions) {
59
            // Don't index assets
60
            if ($this->isAsset($path) && !$this->isExcluded($path)) {
61
                // Check if a file extension is present
62
                $ext = strtolower(pathinfo($path, PATHINFO_EXTENSION));
0 ignored issues
show
Bug introduced by
It seems like pathinfo($path, CliveWal...map\PATHINFO_EXTENSION) can also be of type array; however, parameter $string of strtolower() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

62
                $ext = strtolower(/** @scrutinizer ignore-type */ pathinfo($path, PATHINFO_EXTENSION));
Loading history...
63
                if (in_array($ext, $supported_extensions)) {
64
                    $sitemap->addItem(rtrim($baseUrl, '/').$path, time(), Sitemap::MONTHLY);
65
                }
66
            }
67
        });
68
69
        $sitemap->write();
70
    }
71
72
    private function isExcluded($path)
73
    {
74
        $excluded = $this->jigsaw->getConfig('sitemap.exclude');
75
        $invalidAssets = $excluded ? $excluded->toArray() : [];
76
77
        return Str::contains($path, $invalidAssets);
78
    }
79
80
    private function isAsset($path)
81
    {
82
        return Str::startsWith($path, '/assets');
83
    }
84
}
85