SanitizerBuilder   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
eloc 17
dl 0
loc 65
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setParser() 0 3 1
A registerExtension() 0 3 1
A setLogger() 0 3 1
A build() 0 19 4
1
<?php
2
/**
3
 * (c) Steve Nebes <[email protected]>.
4
 *
5
 *  For the full copyright and license information, please view the LICENSE
6
 *  file that was distributed with this source code.
7
 */
8
9
declare(strict_types=1);
10
11
namespace SN\HtmlSanitizer;
12
13
use SN\HtmlSanitizer\Extension\ExtensionInterface;
14
use SN\HtmlSanitizer\Parser\ParserInterface;
15
use Psr\Log\LoggerInterface;
16
17
/**
18
 * @author Steve Nebes <[email protected]>
19
 *
20
 * @final
21
 */
22
class SanitizerBuilder
23
{
24
    /**
25
     * @var ExtensionInterface[]
26
     */
27
    private $extensions = [];
28
29
    /**
30
     * @var ParserInterface|null
31
     */
32
    private $parser;
33
34
    /**
35
     * @var LoggerInterface|null
36
     */
37
    private $logger;
38
39
    /**
40
     * @param ExtensionInterface $extension
41
     */
42 6
    public function registerExtension(ExtensionInterface $extension)
43
    {
44 6
        $this->extensions[$extension->getName()] = $extension;
45 6
    }
46
47
    /**
48
     * @param ParserInterface|null $parser
49
     */
50 1
    public function setParser(?ParserInterface $parser)
51
    {
52 1
        $this->parser = $parser;
53 1
    }
54
55
    /**
56
     * @param LoggerInterface|null $logger
57
     */
58 2
    public function setLogger(?LoggerInterface $logger)
59
    {
60 2
        $this->logger = $logger;
61 2
    }
62
63
    /**
64
     * @param array $config
65
     *
66
     * @return Sanitizer
67
     */
68 6
    public function build(array $config): Sanitizer
69
    {
70 6
        $nodeVisitors = [];
71
72 6
        foreach ($config['extensions'] ?? [] as $extensionName) {
73 6
            if (!isset($this->extensions[$extensionName])) {
74 1
                throw new \InvalidArgumentException(\sprintf(
75 1
                    'You have requested a non-existent sanitizer extension "%s" (available extensions: %s)',
76 1
                    $extensionName,
77 1
                    \implode(', ', \array_keys($this->extensions))
78
                ));
79
            }
80
81 5
            foreach ($this->extensions[$extensionName]->createNodeVisitors($config) as $tagName => $visitor) {
82 5
                $nodeVisitors[$tagName] = $visitor;
83
            }
84
        }
85
86 5
        return new Sanitizer(new DomVisitor($nodeVisitors), $config['max_input_length'] ?? 4294967295, $this->parser, $this->logger);
87
    }
88
}
89