Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Aug 18, 2016
1 parent bab8711 commit a599f21
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions src/SitemapGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ class SitemapGenerator
protected $crawler;

/** @var callable */
protected $hasCrawled;
protected $shouldCrawl;

/** @var callable */
protected $crawlProfile;
protected $hasCrawled;

/** @var \Spatie\Sitemap\Sitemap */
protected $sitemap;
Expand All @@ -42,12 +42,12 @@ public function __construct(Crawler $crawler)

$this->sitemap = new Sitemap();

$this->hasCrawled = function (Url $url, ResponseInterface $response = null) {
return $url;
$this->shouldCrawl = function (CrawlerUrl $url) {
return $url->host === CrawlerUrl::create($this->url)->host;
};

$this->crawlProfile = function (CrawlerUrl $url) {
return $url->host === CrawlerUrl::create($this->url)->host;
$this->hasCrawled = function (Url $url, ResponseInterface $response = null) {
return $url;
};
}

Expand All @@ -58,6 +58,13 @@ public function setUrl(string $url)
return $this;
}

public function shouldCrawl(callable $shouldCrawl)
{
$this->shouldCrawl = $shouldCrawl;

return $this;
}

public function hasCrawled(callable $hasCrawled)
{
$this->hasCrawled = $hasCrawled;
Expand All @@ -71,21 +78,26 @@ public function hasCrawled(callable $hasCrawled)
public function getSitemap()
{
$this->crawler
->setCrawlProfile($this->getProfile())
->setCrawlObserver($this->getObserver())
->setCrawlProfile($this->getCrawlProfile())
->setCrawlObserver($this->getCrawlObserver())
->startCrawling($this->url);

return $this->sitemap;
}

public function writeToFile($path)
public function writeToFile(string $path)
{
$this->getSitemap()->writeToFile($path);

return $this;
}

protected function getObserver(): Observer
protected function getCrawlProfile(): Profile
{
return new Profile($this->shouldCrawl);
}

protected function getCrawlObserver(): Observer
{
$performAfterUrlHasBeenCrawled = function (CrawlerUrl $crawlerUrl, ResponseInterface $response = null) {
$sitemapUrl = ($this->hasCrawled)(Url::create((string) $crawlerUrl), $response);
Expand All @@ -97,9 +109,4 @@ protected function getObserver(): Observer

return new Observer($performAfterUrlHasBeenCrawled);
}

protected function getProfile(): Profile
{
return new Profile($this->crawlProfile);
}
}

0 comments on commit a599f21

Please sign in to comment.