Test Failed
Push — main ( 961615...d5171d )
by Andreas
12:44 queued 13s
created

Metadata::importFromDump()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
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
    public function __construct(WrapperInterface $wrapper = null)
45
    {
46 19
        $this->wrapper = $wrapper ?: new PdftkWrapper();
47
    }
48 19
49 19
    /**
50
     * Set metadata key/value.
51
     *
52
     * @throws PdfException
53
     */
54
    public function set(string $key, string $value): self
55
    {
56
        $this->checkKey($key);
57
58
        $this->metadata[$key] = $value;
59
60
        return $this;
61 12
    }
62
63 12
    /**
64
     * Get metadata value from key.
65 11
     *
66
     * @return string|bool
67 11
     *
68
     * @throws PdfException
69
     */
70
    public function get(string $key)
71
    {
72
        $this->checkKey($key);
73
74
        if (!isset($this->metadata[$key])) {
75
            return false;
76
        }
77
78
        return $this->metadata[$key];
79 8
    }
80
81 8
    /**
82
     * Unset metadata for key.
83 7
     *
84 2
     * @throws PdfException
85
     */
86
    public function remove(string $key): self
87 6
    {
88
        $this->checkKey($key);
89
90
        unset($this->metadata[$key]);
91
92
        return $this;
93
    }
94
95
    /**
96
     * Checks whether a key is set.
97
     *
98
     * @throws PdfException
99 2
     */
100
    public function has(string $key): bool
101 2
    {
102
        $this->checkKey($key);
103 1
104
        return isset($this->metadata[$key]);
105 1
    }
106
107
    /**
108
     * Returns all current keys.
109
     *
110
     * @return string[]
111
     */
112
    public function keys(): array
113
    {
114
        return array_keys($this->metadata);
115
    }
116
117 2
    /**
118
     * Returns all metadata entries [key => value].
119 2
     */
120
    public function all(): array
121 1
    {
122
        return $this->metadata;
123
    }
124
125
    /**
126
     * Remove all metadata.
127
     */
128
    public function clear(): self
129 2
    {
130
        $this->metadata = [];
131 2
132
        return $this;
133
    }
134
135
    /**
136
     * Apply metadata to file.
137
     *
138
     * @throws PdfException
139 1
     */
140
    public function apply(string $infile, string $outfile = null): self
141 1
    {
142
        $this->wrapper->applyMetadata($this, $infile, $outfile);
143
144
        return $this;
145
    }
146
147
    /**
148
     * Imports metadata from a PDF file.
149 3
     *
150
     * @throws PdfException
151 3
     */
152
    public function import(string $infile): self
153 3
    {
154
        $this->wrapper->importMetadata($this, $infile);
155
156
        return $this;
157
    }
158
159
    /**
160
     * Checks a metadata key.
161
     *
162
     * @throws PdfException
163
     */
164
    private function checkKey(string $key): void
165
    {
166 5
        if (empty($key)) {
167
            throw new PdfException(sprintf('Invalid key name "%s"!', $key));
168 5
        }
169 5
    }
170
}
171