Passed
Branch main (d5171d)
by Andreas
26:52 queued 08:26
created

Metadata::buildInfoBlock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * PDFtk wrapper
4
 *
5
 * @copyright 2014-2024 Institute of Legal Medicine, Medical University of Innsbruck
6
 * @author Martin Pircher <[email protected]>
7
 * @author Andreas Erhard <[email protected]>
8
 * @license LGPL-3.0-only
9
 * @link http://www.gerichtsmedizin.at/
10
 *
11
 * @package pdftk
12
 */
13
14
namespace Gmi\Toolkit\Pdftk;
15
16
use Gmi\Toolkit\Pdftk\Exception\PdfException;
17
18
/**
19
 * Apply metadata to PDF.
20
 */
21
class Metadata
22
{
23
    /**
24
     * InfoBlock
25
     *
26
     * Creator: if the PDF was converted from another format, the application
27
     * used to create the original document.
28
     *
29
     * Producer: if the PDF was converted from another format, the application
30
     * which did the conversion.
31
     *
32
     * @var array
33
     */
34
    private $metadata = [];
35
36
    /**
37
     * @var WrapperInterface
38
     */
39
    private $wrapper;
40
41
    /**
42
     * Constructor.
43
     */
44 29
    public function __construct(WrapperInterface $wrapper = null)
45
    {
46 29
        $this->wrapper = $wrapper ?: new PdftkWrapper();
47 29
    }
48
49
    /**
50
     * Set metadata key/value.
51
     *
52
     * @throws PdfException
53
     */
54 20
    public function set(string $key, string $value): self
55
    {
56 20
        $this->checkKey($key);
57
58 19
        $this->metadata[$key] = $value;
59
60 19
        return $this;
61
    }
62
63
    /**
64
     * Get metadata value from key.
65
     *
66
     * @return string|bool
67
     *
68
     * @throws PdfException
69
     */
70 14
    public function get(string $key)
71
    {
72 14
        $this->checkKey($key);
73
74 13
        if (!isset($this->metadata[$key])) {
75 2
            return false;
76
        }
77
78 12
        return $this->metadata[$key];
79
    }
80
81
    /**
82
     * Unset metadata for key.
83
     *
84
     * @throws PdfException
85
     */
86 2
    public function remove(string $key): self
87
    {
88 2
        $this->checkKey($key);
89
90 1
        unset($this->metadata[$key]);
91
92 1
        return $this;
93
    }
94
95
    /**
96
     * Checks whether a key is set.
97
     *
98
     * @throws PdfException
99
     */
100 2
    public function has(string $key): bool
101
    {
102 2
        $this->checkKey($key);
103
104 1
        return isset($this->metadata[$key]);
105
    }
106
107
    /**
108
     * Returns all current keys.
109
     *
110
     * @return string[]
111
     */
112 2
    public function keys(): array
113
    {
114 2
        return array_keys($this->metadata);
115
    }
116
117
    /**
118
     * Returns all metadata entries [key => value].
119
     */
120 12
    public function all(): array
121
    {
122 12
        return $this->metadata;
123
    }
124
125
    /**
126
     * Remove all metadata.
127
     */
128 3
    public function clear(): self
129
    {
130 3
        $this->metadata = [];
131
132 3
        return $this;
133
    }
134
135
    /**
136
     * Apply metadata to file.
137
     *
138
     * @throws PdfException
139
     */
140 9
    public function apply(string $infile, string $outfile = null): self
141
    {
142 9
        $this->wrapper->applyMetadata($this, $infile, $outfile);
143
144 7
        return $this;
145
    }
146
147
    /**
148
     * Imports metadata from a PDF file.
149
     *
150
     * @throws PdfException
151
     */
152 10
    public function import(string $infile): self
153
    {
154 10
        $this->wrapper->importMetadata($this, $infile);
155
156 10
        return $this;
157
    }
158
159
    /**
160
     * Checks a metadata key.
161
     *
162
     * @throws PdfException
163
     */
164 23
    private function checkKey(string $key): void
165
    {
166 23
        if (empty($key)) {
167 4
            throw new PdfException(sprintf('Invalid key name "%s"!', $key));
168
        }
169 19
    }
170
}
171