DefaultReaderFactory::create()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 15
ccs 7
cts 7
cp 1
rs 10
cc 2
nc 1
nop 4
crap 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 41
    public function __construct(
14
        private readonly XMLReaderFactory $readerFactory,
15
        private readonly DocumentFactory $documentFactory,
16
    ) {
17 41
    }
18
19
    /**
20
     * @throws \Exception
21
     */
22 41
    public function create(string $filepath, ?string $version = null, ?string $encoding = null, ?int $flags = null): Reader
23
    {
24 41
        $xmlReader = $this->readerFactory->create();
25 41
        $document = $this->documentFactory->create($version, $encoding);
26
27 41
        Handler::withErrorHandlerForXMLReader(static function () use ($xmlReader, $filepath, $encoding, $flags): void {
28 41
            $opened = $xmlReader->open($filepath, $encoding, $flags ?? 0);
29
            // @codeCoverageIgnoreStart
30
            if ($opened === false) {
31
                throw new Exception('\XMLReader::open() method failed');
32
            }
33
            // @codeCoverageIgnoreEnd
34 41
        });
35
36 39
        return new DefaultReader($xmlReader, $document);
37
    }
38
}
39