SitemapGenerator   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 118
rs 10
c 0
b 0
f 0
wmc 13

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getCrawlProfile() 0 15 3
A shouldCrawl() 0 5 1
A setConcurrency() 0 3 1
A getCrawlObserver() 0 11 2
A getSitemap() 0 9 1
A hasCrawled() 0 5 1
A __construct() 0 8 1
A writeToFile() 0 5 1
A create() 0 3 1
A setUrl() 0 5 1
1
<?php
2
3
namespace safaeean\Sitemap;
4
5
use safaeean\Crawler\Crawler;
0 ignored issues
show
Bug introduced by
The type safaeean\Crawler\Crawler was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use safaeean\Sitemap\Tags\Url;
7
use safaeean\Sitemap\Crawler\Profile;
8
use safaeean\Sitemap\Crawler\Observer;
9
use safaeean\Crawler\Url as CrawlerUrl;
0 ignored issues
show
Bug introduced by
The type safaeean\Crawler\Url was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Psr\Http\Message\ResponseInterface;
11
12
class SitemapGenerator
13
{
14
    /** @var \safaeean\Sitemap\Sitemap */
15
    protected $sitemap;
16
17
    /** @var string */
18
    protected $urlToBeCrawled = '';
19
20
    /** @var \safaeean\Crawler\Crawler */
21
    protected $crawler;
22
23
    /** @var callable */
24
    protected $shouldCrawl;
25
26
    /** @var callable */
27
    protected $hasCrawled;
28
29
    /** @var int */
30
    protected $concurrency = 10;
31
32
    /**
33
     * @param string $urlToBeCrawled
34
     *
35
     * @return static
36
     */
37
    public static function create(string $urlToBeCrawled)
38
    {
39
        return app(static::class)->setUrl($urlToBeCrawled);
40
    }
41
42
    public function __construct(Crawler $crawler)
43
    {
44
        $this->crawler = $crawler;
45
46
        $this->sitemap = new Sitemap();
47
48
        $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. ( Ignorable by Annotation )

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

48
        $this->hasCrawled = function (Url $url, /** @scrutinizer ignore-unused */ ResponseInterface $response = null) {

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

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