Completed
Push — master ( b86115...3cbedd )
by Freek
03:31
created

MixedContentScanner::setMaximumCrawlCount()   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 Spatie\Crawler\Url;
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
    public function __construct(MixedContentObserver $mixedContentObserver)
23
    {
24
        $this->mixedContentObserver = $mixedContentObserver;
25
    }
26
27
    public function scan(string $url, array $clientOptions = [])
28
    {
29
        $this->guardAgainstInvalidUrl($url);
30
31
        $url = Url::create($url);
32
33
        $crawler = Crawler::create($clientOptions);
34
35
        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...
36
            $crawler->setMaximumCrawlCount($this->maximumCrawlCount);
37
        }
38
39
        $crawler->setCrawlProfile($this->crawlProfile ?? new CrawlInternalUrls($url))
40
            ->setCrawlObserver($this->mixedContentObserver)
41
            ->startCrawling($url);
42
    }
43
44
    public function setCrawlProfile(CrawlProfile $crawlProfile)
45
    {
46
        $this->crawlProfile = $crawlProfile;
47
48
        return $this;
49
    }
50
51
    public function setMaximumCrawlCount(int $maximumCrawlCount)
52
    {
53
        $this->maximumCrawlCount = $maximumCrawlCount;
54
55
        return $this;
56
    }
57
58
    protected function guardAgainstInvalidUrl(string $url)
59
    {
60
        if ($url == '') {
61
            throw InvalidUrl::urlIsEmpty();
62
        }
63
64
        if (!$this->startsWith($url, ['http://', 'https://'])) {
65
            throw InvalidUrl::invalidScheme($url);
66
        }
67
    }
68
69
    protected function startsWith(string $haystack, array $needles): bool
70
    {
71
        foreach ((array)$needles as $needle) {
72
            if ($needle != '' && substr($haystack, 0, strlen($needle)) === (string)$needle) {
73
                return true;
74
            }
75
        }
76
77
        return false;
78
    }
79
}
80