Metadata   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 18
c 1
b 0
f 0
dl 0
loc 44
ccs 11
cts 11
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A save() 0 6 2
A __construct() 0 9 2
A getOpfFilePath() 0 3 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