Completed
Pull Request — master (#27)
by
unknown
06:13
created

MixedContentObserver::linkedCssFound()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace Spatie\MixedContentScanner;
4
5
use Spatie\Crawler\CrawlObserver;
6
use Psr\Http\Message\UriInterface;
7
use Psr\Http\Message\ResponseInterface;
8
use GuzzleHttp\Exception\RequestException;
9
10
class MixedContentObserver extends CrawlObserver
11
{
12
    private $crawledCss = [];
13
14
    private $shouldExtractLinkedCss = false;
15
16
    public function crawled(UriInterface $url, ResponseInterface $response, ?UriInterface $foundOnUrl = null)
17
    {
18
        list($mixedContent, $linkedCss) = MixedContentExtractor::extract((string) $response->getBody(), $url);
19
20
        if ($this->shouldExtractLinkedCss) {
21
            foreach ($linkedCss as $css) {
22
                $mixedContent = array_merge((array) $mixedContent, (array) $this->linkedCssFound($css, $url));
23
            }
24
        }
25
26
        if (! count($mixedContent)) {
27
            $this->noMixedContentFound($url);
28
29
            return;
30
        }
31
32
        foreach ($mixedContent as $mixedContentItem) {
33
            $this->mixedContentFound($mixedContentItem);
34
        }
35
    }
36
37
    public function crawlFailed(
38
        UriInterface $url,
39
        RequestException $requestException,
40
        ?UriInterface $foundOnUrl = null
41
    ) {
42
    }
43
44
    /**
45
     * Will be called when mixed content was found.
46
     *
47
     * @param \Spatie\MixedContentScanner\MixedContent $mixedContent
48
     */
49
    public function mixedContentFound(MixedContent $mixedContent)
0 ignored issues
show
Unused Code introduced by
The parameter $mixedContent 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
    {
51
    }
52
53
    /**
54
     * Will be called when no mixed content was found on the given url.
55
     *
56
     * @param \Psr\Http\Message\UriInterface
57
     */
58
    public function noMixedContentFound(UriInterface $crawledUrl)
0 ignored issues
show
Unused Code introduced by
The parameter $crawledUrl 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...
59
    {
60
    }
61
62
    public function linkedCssFound(UriInterface $css, UriInterface $linkedByUrl)
63
    {
64
        if (!in_array($css, $this->crawledCss)) {
65
            $this->crawledCss[] = $css;
66
            return MixedContentLinkedCssExtractor::extract(Body::get($css), $css, $linkedByUrl);
67
        }
68
    }
69
70
    public function withLinkedCss()
71
    {
72
        $this->shouldExtractLinkedCss = true;
73
        return $this;
74
    }
75
}
76