Passed
Push — 2.3 ( 32a49e )
by Colin
03:51 queued 01:09
created

DomainFilteringAdapter   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 33
ccs 9
cts 9
cp 1
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A updateEmbeds() 0 4 1
A __construct() 0 4 1
A createRegex() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the league/commonmark package.
7
 *
8
 * (c) Colin O'Dell <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace League\CommonMark\Extension\Embed;
15
16
class DomainFilteringAdapter implements EmbedAdapterInterface
17
{
18
    private EmbedAdapterInterface $decorated;
19
20
    private string $regex;
21
22
    /**
23
     * @param string[] $allowedDomains
24
     */
25 6
    public function __construct(EmbedAdapterInterface $decorated, array $allowedDomains)
26
    {
27 6
        $this->decorated = $decorated;
28 6
        $this->regex     = self::createRegex($allowedDomains);
29
    }
30
31
    /**
32
     * {@inheritDoc}
33
     */
34 6
    public function updateEmbeds(array $embeds): void
35
    {
36 6
        $this->decorated->updateEmbeds(\array_filter($embeds, function (Embed $embed): bool {
37 6
            return \preg_match($this->regex, $embed->getUrl()) === 1;
38
        }));
39
    }
40
41
    /**
42
     * @param string[] $allowedDomains
43
     */
44 6
    private static function createRegex(array $allowedDomains): string
45
    {
46 6
        $allowedDomains = \array_map('preg_quote', $allowedDomains);
47
48 6
        return '/^(?:https?:\/\/)?(?:[^.]+\.)*(' . \implode('|', $allowedDomains) . ')/';
49
    }
50
}
51