Completed
Push — master ( 83739a...a13ee4 )
by Colin
14s queued 11s
created

ExternalLinkProcessor::__invoke()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 16
cts 16
cp 1
rs 8.8497
c 0
b 0
f 0
cc 6
nc 5
nop 1
crap 6
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace League\CommonMark\Extension\ExternalLink;
13
14
use League\CommonMark\EnvironmentInterface;
15
use League\CommonMark\Event\DocumentParsedEvent;
16
use League\CommonMark\Inline\Element\Link;
17
18
final class ExternalLinkProcessor
19
{
20
    /** @var EnvironmentInterface */
21
    private $environment;
22
23
    /**
24
     * @param EnvironmentInterface $environment
25
     */
26 9
    public function __construct(EnvironmentInterface $environment)
27
    {
28 9
        $this->environment = $environment;
29 9
    }
30
31
    /**
32
     * @param DocumentParsedEvent $e
33
     */
34 9
    public function __invoke(DocumentParsedEvent $e)
35
    {
36 9
        $internalHosts = $this->environment->getConfig('external_link/internal_hosts', []);
37 9
        $openInNewWindow = $this->environment->getConfig('external_link/open_in_new_window', false);
38 9
        $classes = $this->environment->getConfig('external_link/html_class', '');
39
40 9
        $walker = $e->getDocument()->walker();
41 9
        while ($event = $walker->next()) {
42 9
            if ($event->isEntering() && $event->getNode() instanceof Link) {
43
                /** @var Link $link */
44 9
                $link = $event->getNode();
45
46 9
                $host = parse_url($link->getUrl(), PHP_URL_HOST);
47 9
                if (empty($host)) {
48
                    // Something is terribly wrong with this URL
49 3
                    continue;
50
                }
51
52 6
                if (self::hostMatches($host, $internalHosts)) {
53 3
                    $link->data['external'] = false;
54 3
                    continue;
55
                }
56
57
                // Host does not match our list
58 6
                $this->markLinkAsExternal($link, $openInNewWindow, $classes);
59
            }
60
        }
61 9
    }
62
63
    /**
64
     * @param Link   $link
65
     * @param bool   $openInNewWindow
66
     * @param string $classes
67
     */
68 6
    private function markLinkAsExternal(Link $link, bool $openInNewWindow, string $classes): void
69
    {
70 6
        $link->data['external'] = true;
71 6
        $link->data['attributes'] = $link->getData('attributes', []);
72 6
        $link->data['attributes']['rel'] = 'noopener noreferrer';
73
74 6
        if ($openInNewWindow) {
75 3
            $link->data['attributes']['target'] = '_blank';
76
        }
77
78 6
        if (!empty($classes)) {
79 3
            $link->data['attributes']['class'] = trim(($link->data['attributes']['class'] ?? '') . ' ' . $classes);
80
        }
81 6
    }
82
83
    /**
84
     * @param string $host
85
     * @param mixed  $compareTo
86
     *
87
     * @return bool
88
     *
89
     * @internal This method is only public so we can easily test it. DO NOT USE THIS OUTSIDE OF THIS EXTENSION!
90
     */
91 27
    public static function hostMatches(string $host, $compareTo)
92
    {
93 27
        foreach ((array) $compareTo as $c) {
94 24
            if (strpos($c, '/') === 0) {
95 6
                if (preg_match($c, $host)) {
96 6
                    return true;
97
                }
98 18
            } elseif ($c === $host) {
99 9
                return true;
100
            }
101
        }
102
103 15
        return false;
104
    }
105
}
106