InteractsWithDescription::getDescription()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 10
cc 4
nc 2
nop 0
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpEpub\Traits;
6
7
use SimpleXMLElement;
8
9
trait InteractsWithDescription
10
{
11
    private readonly SimpleXMLElement $opfXml;
12
    private readonly string $dcNamespace;
13
14
    /**
15
     * Gets the description of the EPUB.
16
     */
17 9
    public function getDescription(): string
18
    {
19 9
        $this->opfXml->registerXPathNamespace('dc', $this->dcNamespace);
20
21 9
        $descriptionNode = $this->opfXml->xpath('//dc:description');
22
23 9
        if ($descriptionNode === false || $descriptionNode === null || $descriptionNode === []) {
24 6
            return '';
25
        }
26
27 3
        return (string) $descriptionNode[0];
28
    }
29
30
    /**
31
     * Sets the description of the EPUB.
32
     */
33 1
    public function setDescription(string $description): void
34
    {
35 1
        $this->opfXml->registerXPathNamespace('dc', $this->dcNamespace);
36
37 1
        $descriptionNode = $this->opfXml->xpath('//dc:description');
38
39 1
        if ($descriptionNode !== false && $descriptionNode !== null && $descriptionNode !== [] && isset($descriptionNode[0])) {
40
            // @phpstan-ignore-next-line
41 1
            $descriptionNode[0][0] = $description;
42
        } else {
43
            $this->opfXml->metadata->addChild('description', $description, $this->dcNamespace);
44
        }
45
    }
46
}
47