Passed
Pull Request — develop (#234)
by Mikaël
49:21 queued 46:19
created

AbstractDocument::initContentFromContentString()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 8
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 13
ccs 8
cts 8
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WsdlToPhp\PackageGenerator\Model;
6
7
use DOMDocument;
8
use Exception;
9
use InvalidArgumentException;
10
use WsdlToPhp\PackageGenerator\Generator\Generator;
11
use WsdlToPhp\WsdlHandler\AbstractDocument as AbstractDocumentHandler;
12
13
abstract class AbstractDocument extends AbstractModel
14
{
15
    protected AbstractDocumentHandler $content;
16
17 354
    public function __construct(Generator $generator, string $name, string $content)
18
    {
19 354
        parent::__construct($generator, $name);
20 354
        $this->initContentFromContentString($content);
21 352
    }
22
23 114
    public function getContent(): AbstractDocumentHandler
24
    {
25 114
        return $this->content;
26
    }
27
28
    abstract protected function contentClass(): string;
29
30 354
    protected function initContentFromContentString(string $content): AbstractDocument
31
    {
32 354
        $contentClass = $this->contentClass();
33 354
        $domDocument = new DOMDocument('1.0', 'utf-8');
34
35
        try {
36 354
            $domDocument->loadXML($content, LIBXML_NOERROR);
37 354
            $this->content = new $contentClass($domDocument, $this->generator);
38 2
        } catch (Exception $exception) {
39 2
            throw new InvalidArgumentException(sprintf('Unable to load document at "%s"', $this->getName()), __LINE__, $exception);
40
        }
41
42 352
        return $this;
43
    }
44
45 2
    protected function toJsonSerialize(): array
46
    {
47 2
        return [];
48
    }
49
}
50