Completed
Push — master ( 73bf73...b0bde5 )
by Freek
01:17
created

MixedContentScanner::configureCrawler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Spatie\MixedContentScanner;
4
5
use GuzzleHttp\Psr7\Uri;
6
use Spatie\Crawler\Crawler;
7
use Spatie\Crawler\CrawlProfile;
8
use Spatie\Crawler\CrawlInternalUrls;
9
use Spatie\MixedContentScanner\Exceptions\InvalidUrl;
10
11
class MixedContentScanner
12
{
13
    /** @var \Spatie\MixedContentScanner\MixedContentObserver */
14
    public $mixedContentObserver;
15
16
    /** @var null|\Spatie\Crawler\CrawlProfile */
17
    public $crawlProfile;
18
19
    /** @var int|null */
20
    protected $maximumCrawlCount;
21
22
    /** @var callable */
23
    protected $configureCrawler;
24
25
    public function __construct(MixedContentObserver $mixedContentObserver)
26
    {
27
        $this->mixedContentObserver = $mixedContentObserver;
28
29
        $this->configureCrawler = function (Crawler $crawler) {};
0 ignored issues
show
Unused Code introduced by
The parameter $crawler 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...
30
    }
31
32
    public function scan(string $url, array $clientOptions = [])
33
    {
34
        $this->guardAgainstInvalidUrl($url);
35
36
        $url = new Uri($url);
37
38
        $crawler = Crawler::create($clientOptions);
39
40
        ($this->configureCrawler)($crawler);
41
42
        if ($this->maximumCrawlCount) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->maximumCrawlCount of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
43
            $crawler->setMaximumCrawlCount($this->maximumCrawlCount);
44
        }
45
46
        $crawler->setCrawlProfile($this->crawlProfile ?? new CrawlInternalUrls($url))
47
            ->setCrawlObserver($this->mixedContentObserver)
48
            ->startCrawling($url);
49
    }
50
51
    public function configureCrawler(callable $callable)
52
    {
53
        $this->configureCrawler = $callable;
54
55
        return $this;
56
    }
57
58
    public function setCrawlProfile(CrawlProfile $crawlProfile)
59
    {
60
        $this->crawlProfile = $crawlProfile;
61
62
        return $this;
63
    }
64
65
    /**
66
     * @deprecated You can set this via configureCrawler
67
     */
68
    public function setMaximumCrawlCount(int $maximumCrawlCount)
69
    {
70
        $this->maximumCrawlCount = $maximumCrawlCount;
71
72
        return $this;
73
    }
74
75
    protected function guardAgainstInvalidUrl(string $url)
76
    {
77
        if ($url == '') {
78
            throw InvalidUrl::urlIsEmpty();
79
        }
80
81
        if (!$this->startsWith($url, ['http://', 'https://'])) {
82
            throw InvalidUrl::invalidScheme($url);
83
        }
84
    }
85
86
    protected function startsWith(string $haystack, array $needles): bool
87
    {
88
        foreach ((array)$needles as $needle) {
89
            if ($needle != '' && substr($haystack, 0, strlen($needle)) === (string)$needle) {
90
                return true;
91
            }
92
        }
93
94
        return false;
95
    }
96
}
97