Completed
Push — master ( 28361e...717ffa )
by Ralf
15s queued 11s
created

Mets   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 73
dl 0
loc 146
rs 10
c 0
b 0
f 0
wmc 14

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getMetsXpath() 0 3 1
A __construct() 0 3 1
A getMetsXml() 0 3 1
A getState() 0 8 1
A getSlub() 0 12 1
A setMetsXml() 0 5 1
A getMods() 0 12 1
B getFiles() 0 76 7
1
<?php
2
namespace EWW\Dpf\Helper;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
class Mets
18
{
19
20
    /**
21
     * mets
22
     *
23
     * @var \DOMDocument
24
     */
25
    protected $metsDom;
26
27
    public function __construct($metsXml)
28
    {
29
        $this->setMetsXml($metsXml);
30
    }
31
32
    public function setMetsXml($metsXml)
33
    {
34
        $metsDom = new \DOMDocument();
35
        $metsDom->loadXML($metsXml);
36
        $this->metsDom = $metsDom;
37
    }
38
39
    public function getMetsXml()
40
    {
41
        return $this->metsDom->saveXML();
42
    }
43
44
    public function getMetsXpath()
45
    {
46
        return new \DOMXPath($this->metsDom);
47
    }
48
49
    public function getMods()
50
    {
51
        $xpath = $this->getMetsXpath();
52
53
        $xpath->registerNamespace("mods", "http://www.loc.gov/mods/v3");
54
        $modsNodes = $xpath->query("/mets:mets/mets:dmdSec/mets:mdWrap/mets:xmlData/mods:mods");
55
56
        $modsXml = $this->metsDom->saveXML($modsNodes->item(0));
57
58
        $mods = new Mods($modsXml);
59
60
        return $mods;
61
    }
62
63
    public function getSlub()
64
    {
65
        $xpath = $this->getMetsXpath();
66
67
        $xpath->registerNamespace("slub", "http://slub-dresden.de/");
68
        $slubNodes = $xpath->query("/mets:mets/mets:amdSec/mets:techMD/mets:mdWrap/mets:xmlData/slub:info");
69
70
        $slubXml = $this->metsDom->saveXML($slubNodes->item(0));
71
72
        $slub = new Slub($slubXml);
73
74
        return $slub;
75
    }
76
77
    public function getState()
78
    {
79
80
        $xpath = $this->getMetsXpath();
81
        $xpath->registerNamespace("mets", "http://www.loc.gov/METS/");
82
83
        $dmdSec = $xpath->query("/mets:mets/mets:dmdSec");
84
        return $dmdSec->item(0)->getAttribute("STATUS");
85
    }
86
87
    public function getFiles()
88
    {
89
        $xpath = $this->getMetsXpath();
90
91
        $xpath->registerNamespace("xlink", "http://www.w3.org/1999/xlink");
92
93
        $fileNodesOriginal = $xpath->query('/mets:mets/mets:fileSec/mets:fileGrp[@USE="ORIGINAL"]/mets:file');
94
        $fileNodesDownload = $xpath->query('/mets:mets/mets:fileSec/mets:fileGrp[@USE="DOWNLOAD"]/mets:file');
95
        $fileNodesDeleted = $xpath->query('/mets:mets/mets:fileSec/mets:fileGrp[@USE="DELETED"]/mets:file');
96
97
        $files = array();
98
99
        foreach ($fileNodesOriginal as $item) {
100
101
            $xlinkNS = "http://www.w3.org/1999/xlink";
102
            $mextNS  = "http://slub-dresden.de/mets";
103
104
            $flocat = $xpath->query('mets:FLocat', $item);
105
            if ($flocat->length > 0) {
106
                $href = $flocat->item(0)->getAttributeNS($xlinkNS, "href");
107
            }
108
109
            $files[] = array(
110
                'id'       => $item->getAttribute("ID"),
111
                'mimetype' => $item->getAttribute("MIMETYPE"),
112
                'href'     => $href,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $href does not seem to be defined for all execution paths leading up to this point.
Loading history...
113
                'title'    => $item->getAttributeNS($mextNS, "LABEL"),
114
                'archive'  => ($item->getAttribute("USE") == 'ARCHIVE'),
115
                'download' => false,
116
                'deleted'  => false,
117
            );
118
        }
119
120
        foreach ($fileNodesDownload as $item) {
121
122
            $xlinkNS = "http://www.w3.org/1999/xlink";
123
            $mextNS  = "http://slub-dresden.de/mets";
124
125
            $flocat = $xpath->query('mets:FLocat', $item);
126
            if ($flocat->length > 0) {
127
                $href = $flocat->item(0)->getAttributeNS($xlinkNS, "href");
128
            }
129
130
            $files[] = array(
131
                'id'       => $item->getAttribute("ID"),
132
                'mimetype' => $item->getAttribute("MIMETYPE"),
133
                'href'     => $href,
134
                'title'    => $item->getAttributeNS($mextNS, "LABEL"),
135
                'archive'  => ($item->getAttribute("USE") == 'ARCHIVE'),
136
                'download' => true,
137
                'deleted'  => false,
138
            );
139
        }
140
141
        foreach ($fileNodesDeleted as $item) {
142
143
            $xlinkNS = "http://www.w3.org/1999/xlink";
144
            $mextNS  = "http://slub-dresden.de/mets";
145
146
            $flocat = $xpath->query('mets:FLocat', $item);
147
            if ($flocat->length > 0) {
148
                $href = $flocat->item(0)->getAttributeNS($xlinkNS, "href");
149
            }
150
151
            $files[] = array(
152
                'id'       => $item->getAttribute("ID"),
153
                'mimetype' => $item->getAttribute("MIMETYPE"),
154
                'href'     => $href,
155
                'title'    => $item->getAttributeNS($mextNS, "LABEL"),
156
                'archive'  => ($item->getAttribute("USE") == 'ARCHIVE'),
157
                'download' => false,
158
                'deleted'  => true,
159
            );
160
        }
161
162
        return $files;
163
164
    }
165
166
}
167