Passed
Branch master (31b9e5)
by Tomáš
01:44
created

DefaultReaderFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 15 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Inspirum\XML\Reader;
6
7
use Exception;
8
use Inspirum\XML\Builder\DocumentFactory;
9
use Inspirum\XML\Exception\Handler;
10
11
final class DefaultReaderFactory implements ReaderFactory
12
{
13 16
    public function __construct(
14
        private XMLReaderFactory $readerFactory,
15
        private DocumentFactory $documentFactory,
16
    ) {
17
    }
18
19 16
    public function create(string $filepath, ?string $version = null, ?string $encoding = null, ?int $flags = null): Reader
20
    {
21 16
        $xmlReader = $this->readerFactory->create();
22 16
        $document  = $this->documentFactory->create($version, $encoding);
23
24 16
        Handler::withErrorHandlerForXMLReader(static function () use ($xmlReader, $filepath, $encoding, $flags): void {
25 16
            $opened = $xmlReader->open($filepath, $encoding, $flags ?? 0);
26
            // @codeCoverageIgnoreStart
27
            if ($opened === false) {
28
                throw new Exception('\XMLReader::open() method failed');
29
            }
30
            // @codeCoverageIgnoreEnd
31
        });
32
33 14
        return new DefaultReader($xmlReader, $document);
34
    }
35
}
36