Metadata::getOpfFilePath()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpEpub;
6
7
use SimpleXMLElement;
8
9
class Metadata
10
{
11
    use Traits\InteractsWithTitle;
12
    use Traits\InteractsWithDescription;
13
    use Traits\InteractsWithDate;
14
    use Traits\InteractsWithAuthors;
15
    use Traits\InteractsWithPublisher;
16
    use Traits\InteractsWithLanguage;
17
    use Traits\InteractsWithSubject;
18
    use Traits\InteractsWithIdentifier;
19
20
    private readonly string $dcNamespace;
21
22
    /**
23
     * Metadata constructor.
24
     */
25 40
    public function __construct(private readonly SimpleXMLElement $opfXml, private string $opfFilePath)
26
    {
27 40
        $namespaces = $this->opfXml->getNamespaces(true);
28
29 40
        if (! isset($namespaces['dc'])) {
30 1
            throw new Exception('Failed to identify dc namespace');
31
        }
32
33 39
        $this->dcNamespace = $namespaces['dc'];
34
    }
35
36
    /**
37
     * Saves the updated OPF file.
38
     *
39
     * @throws Exception If the OPF file cannot be saved.
40
     */
41 9
    public function save(): void
42
    {
43 9
        $result = $this->opfXml->asXML($this->opfFilePath);
44
45 9
        if ($result === false) {
46 1
            throw new Exception("Failed to save OPF file: {$this->opfFilePath}");
47
        }
48
    }
49
50 1
    public function getOpfFilePath(): string
51
    {
52 1
        return $this->opfFilePath;
53
    }
54
}
55