Completed
Push — master ( bab871...c442ff )
by Freek
03:14 queued 12s
created

SitemapGenerator::shouldCrawl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Spatie\Sitemap;
4
5
use Psr\Http\Message\ResponseInterface;
6
use Spatie\Crawler\Crawler;
7
use Spatie\Crawler\Url as CrawlerUrl;
8
use Spatie\Sitemap\Crawler\Observer;
9
use Spatie\Sitemap\Crawler\Profile;
10
use Spatie\Sitemap\Tags\Url;
11
12
class SitemapGenerator
13
{
14
    /** @var string */
15
    protected $url = '';
16
17
    /** @var \Spatie\Crawler\Crawler */
18
    protected $crawler;
19
20
    /** @var callable */
21
    protected $shouldCrawl;
22
23
    /** @var callable */
24
    protected $hasCrawled;
25
26
    /** @var \Spatie\Sitemap\Sitemap */
27
    protected $sitemap;
28
29
    /**
30
     * @param string $url
31
     *
32
     * @return static
33
     */
34
    public static function create(string $url)
35
    {
36
        return app(static::class)->setUrl($url);
37
    }
38
39
    public function __construct(Crawler $crawler)
40
    {
41
        $this->crawler = $crawler;
42
43
        $this->sitemap = new Sitemap();
44
45
        $this->shouldCrawl = function (CrawlerUrl $url) {
0 ignored issues
show
Unused Code introduced by
The parameter $url is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
46
            return true;
47
        };
48
49
        $this->hasCrawled = function (Url $url, ResponseInterface $response = null) {
0 ignored issues
show
Unused Code introduced by
The parameter $response is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
50
            return $url;
51
        };
52
    }
53
54
    public function setUrl(string $url)
55
    {
56
        $this->url = $url;
57
58
        return $this;
59
    }
60
61
    public function shouldCrawl(callable $shouldCrawl)
62
    {
63
        $this->shouldCrawl = $shouldCrawl;
64
65
        return $this;
66
    }
67
68
    public function hasCrawled(callable $hasCrawled)
69
    {
70
        $this->hasCrawled = $hasCrawled;
71
72
        return $this;
73
    }
74
75
    /**
76
     * @return \Spatie\Sitemap\Sitemap
77
     */
78
    public function getSitemap()
79
    {
80
        $this->crawler
81
            ->setCrawlProfile($this->getCrawlProfile())
82
            ->setCrawlObserver($this->getCrawlObserver())
83
            ->startCrawling($this->url);
84
85
        return $this->sitemap;
86
    }
87
88
    public function writeToFile(string $path)
89
    {
90
        $this->getSitemap()->writeToFile($path);
91
92
        return $this;
93
    }
94
95
    protected function getCrawlProfile(): Profile
96
    {
97
        $shouldCrawl = function(CrawlerUrl $url) {
98
            if ($url->host !== CrawlerUrl::create($this->url)->host) {
99
                return false;
100
            }
101
102
            return $this->shouldCrawl;
103
        };
104
105
        return new Profile($shouldCrawl);
106
    }
107
108
    protected function getCrawlObserver(): Observer
109
    {
110
        $performAfterUrlHasBeenCrawled = function (CrawlerUrl $crawlerUrl, ResponseInterface $response = null) {
111
            $sitemapUrl = ($this->hasCrawled)(Url::create((string) $crawlerUrl), $response);
112
113
            if ($sitemapUrl) {
114
                $this->sitemap->add($sitemapUrl);
115
            }
116
        };
117
118
        return new Observer($performAfterUrlHasBeenCrawled);
119
    }
120
}
121