Completed
Push — master ( 3cbedd...c51217 )
by Freek
03:22
created

MixedContentExtractor   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 61
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getSearchNodes() 0 18 1
B extract() 0 28 5
A isShortLink() 0 10 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
                    ->reduce(function (DomCrawler $node) {
19
                        return ! self::isShortLink($node);
20
                    })
21
                    ->each(function (DomCrawler $node) use ($tagName, $attribute) {
22
                        $url = Url::create($node->attr($attribute));
23
24
                        if ($tagName === 'link' && $attribute === 'href') {
25
                            if ($node->attr('rel') !== 'stylesheet') {
26
                                return null;
27
                            }
28
                        }
29
30
                        return $url->scheme === 'http' ? [$tagName, $url] : null;
31
                    });
32
            })
33
            ->flatten(1)
34
            ->filter()
35
            ->mapSpread(function ($tagName, $mixedContentUrl) use ($currentUri) {
36
                return new MixedContent($tagName, $mixedContentUrl, Url::create($currentUri));
37
            })
38
            ->toArray();
39
    }
40
41
    protected static function getSearchNodes(): Collection
42
    {
43
        return collect([
44
            ['audio', 'src'],
45
            ['embed', 'src'],
46
            ['form', 'action'],
47
            ['link', 'href'],
48
            ['iframe', 'src'],
49
            ['img', 'src'],
50
            ['img', 'srcset'],
51
            ['object', 'data'],
52
            ['param', 'value'],
53
            ['script', 'src'],
54
            ['source', 'src'],
55
            ['source', 'srcset'],
56
            ['video', 'src'],
57
        ]);
58
    }
59
60
    protected static function isShortLink(DomCrawler $node)
61
    {
62
        $relAttribute = $node->getNode(0)->attributes->getNamedItem('rel');
63
64
        if (is_null($relAttribute)) {
65
            return false;
66
        }
67
68
        return strtolower($relAttribute->nodeValue) === 'shortlink';
69
    }
70
}
71