Completed
Push — master ( 660bec...b700ce )
by Freek
11s
created

MixedContentExtractor::extract()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 13
nc 1
nop 2
1
<?php
2
3
namespace Spatie\MixedContentScanner;
4
5
use Spatie\Crawler\Url;
6
use Illuminate\Support\Collection;
7
use Symfony\Component\DomCrawler\Link;
8
use Symfony\Component\DomCrawler\Crawler as DomCrawler;
9
10
class MixedContentExtractor
11
{
12
    public static function extract(string $html, string $currentUri): array
13
    {
14
        return static::getSearchNodes()
15
            ->mapSpread(function ($tagName, $attribute) use ($html, $currentUri) {
16
                return (new DomCrawler($html, $currentUri))
17
                    ->filterXPath("//{$tagName}[@{$attribute}]")
18
                    ->each(function (DomCrawler $node) use ($tagName, $attribute) {
19
                        $url = Url::create($node->attr($attribute));
20
21
                        return $url->scheme === 'http' ? [$tagName, $url] : null;
22
                    });
23
            })
24
            ->flatten(1)
25
            ->filter()
26
            ->mapSpread(function ($tagName, $mixedContentUrl) use ($currentUri) {
27
                return new MixedContent($tagName, $mixedContentUrl, Url::create($currentUri));
28
            })
29
            ->toArray();
30
    }
31
32
    protected static function getSearchNodes(): Collection
33
    {
34
        return collect([
35
            ['audio', 'src'],
36
            ['embed', 'src'],
37
            ['form', 'action'],
38
            ['link', 'href'],
39
            ['iframe', 'src'],
40
            ['img', 'src'],
41
            ['img', 'srcset'],
42
            ['object', 'data'],
43
            ['param', 'value'],
44
            ['script', 'src'],
45
            ['source', 'src'],
46
            ['source', 'srcset'],
47
            ['video', 'src'],
48
        ]);
49
    }
50
}
51