Passed
Pull Request — master (#173)
by
unknown
11:53
created

Mods   B

Complexity

Total Complexity 45

Size/Duplication

Total Lines 287
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 117
dl 0
loc 287
rs 8.8
c 1
b 0
f 0
wmc 45

19 Methods

Rating   Name   Duplication   Size   Complexity  
A setModsXml() 0 9 3
A getModsXml() 0 3 1
A getTitle() 0 8 2
A getModsXpath() 0 4 1
A __construct() 0 3 1
A getOriginalSourceTitle() 0 8 2
A getPublishingYear() 0 8 2
A addQucosaUrn() 0 11 2
A getQucosaUrn() 0 11 3
A clearAllUrn() 0 11 3
A hasQucosaUrn() 0 12 3
A getPersons() 0 28 4
A getSourceDetails() 0 27 5
A getAuthors() 0 3 1
A removeDateIssued() 0 6 2
A setTitle() 0 10 3
A getDateIssued() 0 9 2
A getPublishers() 0 3 1
A setDateIssued() 0 34 4

How to fix   Complexity   

Complex Class

Complex classes like Mods often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Mods, and based on these observations, apply Extract Interface, too.

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
18
class Mods
19
{
20
    /**
21
     * metadataObjectRepository
22
     *
23
     * @var \EWW\Dpf\Domain\Repository\MetadataObjectRepository
24
     * @inject
25
     */
26
    protected $metadataObjectRepository = null;
27
28
    /**
29
     * metadatGroupRepository
30
     *
31
     * @var \EWW\Dpf\Domain\Repository\MetadataGroupRepository
32
     * @inject
33
     */
34
    protected $metadataGroupRepository = null;
35
36
37
    protected $modsDom;
38
39
    public function __construct($modsXml)
40
    {
41
        $this->setModsXml($modsXml);
42
    }
43
44
    public function setModsXml($modsXml)
45
    {
46
        $modsDom = new \DOMDocument();
47
        if (!empty($modsXml)) {
48
            if (is_null(@$modsDom->loadXML($modsXml))) {
49
                throw new \Exception("Couldn't load MODS data!");
50
            }
51
        }
52
        $this->modsDom = $modsDom;
53
    }
54
55
    public function getModsXml()
56
    {
57
        return $this->modsDom->saveXML();
58
    }
59
60
    public function getModsXpath()
61
    {
62
        $xpath = \EWW\Dpf\Helper\XPath::create($this->modsDom);
63
        return $xpath;
64
    }
65
66
    public function getTitle()
67
    {
68
        $titleNode = $this->getModsXpath()->query('/mods:mods/mods:titleInfo[@usage="primary"]/mods:title');
69
70
        if ($titleNode->length == 0) {
71
            $titleNode = $this->getModsXpath()->query("/mods:mods/mods:titleInfo/mods:title");
72
        }
73
        return $titleNode->item(0)->nodeValue;
74
    }
75
76
77
    public function setTitle($title)
78
    {
79
        $titleNode = $this->getModsXpath()->query('/mods:mods/mods:titleInfo[@usage="primary"]/mods:title');
80
81
        if ($titleNode->length == 0) {
82
            $titleNode = $this->getModsXpath()->query("/mods:mods/mods:titleInfo/mods:title");
83
        }
84
85
        if ($titleNode->length > 0) {
86
            $titleNode->item(0)->nodeValue = $title;
87
        }
88
    }
89
90
91
    public function getAuthors()
92
    {
93
        return $this->getPersons("aut");
94
    }
95
96
    public function getPublishers()
97
    {
98
        return $this->getPersons("edt");
99
    }
100
101
    /**
102
     * Get persons of the given role
103
     *
104
     * @param $role
105
     * @return array
106
     */
107
    protected function getPersons($role)
108
    {
109
        $xpath = $this->getModsXpath();
110
111
        $authorNode = $xpath->query('/mods:mods/mods:name[mods:role/mods:roleTerm[@type="code"]="'.$role.'"]');
112
113
        $authors = array();
114
115
        foreach ($authorNode as $key => $author) {
116
117
            $familyNodes = $xpath->query('mods:namePart[@type="family"]', $author);
118
119
            $givenNodes = $xpath->query('mods:namePart[@type="given"]', $author);
120
121
            $name = array();
122
123
            if ($givenNodes->length > 0) {
124
                $name[] = $givenNodes->item(0)->nodeValue;
125
            }
126
127
            if ($familyNodes->length > 0) {
128
                $name[] = $familyNodes->item(0)->nodeValue;
129
            }
130
131
            $authors[$key] = implode(" ", $name);
132
        }
133
134
        return $authors;
135
    }
136
137
    public function setDateIssued($date)
138
    {
139
140
        $originInfo = $this->getModsXpath()->query('/mods:mods/mods:originInfo[@eventType="distribution"]');
141
142
        if ($originInfo->length > 0) {
143
            $dateIssued = $this->getModsXpath()->query('mods:dateIssued[@encoding="iso8601"][@keyDate="yes"]', $originInfo->item(0));
144
145
            if ($dateIssued->length == 0) {
146
                $newDateIssued = $this->modsDom->createElement('mods:dateIssued');
147
                $newDateIssued->setAttribute('encoding', 'iso8601');
148
                $newDateIssued->setAttribute('keyDate', 'yes');
149
                $newDateIssued->nodeValue = $date;
150
                $originInfo->item(0)->appendChild($newDateIssued);
151
            } else {
152
                $dateIssued->item(0)->nodeValue = $date;
153
            }
154
155
        } else {
156
157
            $rootNode = $this->getModsXpath()->query('/mods:mods');
158
159
            if ($rootNode->length == 1) {
160
                $newOriginInfo = $this->modsDom->createElement('mods:originInfo');
161
                $newOriginInfo->setAttribute('eventType', 'distribution');
162
                $rootNode->item(0)->appendChild($newOriginInfo);
163
164
                $newDateIssued = $this->modsDom->createElement('mods:dateIssued');
165
                $newDateIssued->setAttribute('encoding', 'iso8601');
166
                $newDateIssued->setAttribute('keyDate', 'yes');
167
                $newDateIssued->nodeValue = $date;
168
                $newOriginInfo->appendChild($newDateIssued);
169
            } else {
170
                throw new \Exception('Invalid xml data.');
171
            }
172
173
        }
174
175
    }
176
177
178
    public function getPublishingYear()
179
    {
180
        $year = $this->getModsXpath()->query('/mods:mods/mods:originInfo[@eventType="publication"]/mods:dateIssued[@encoding="iso8601"]');
181
        if ($year->length > 0) {
182
            return $year->item(0)->nodeValue;
183
        }
184
185
        return null;
186
    }
187
188
    public function getOriginalSourceTitle()
189
    {
190
        $node= $this->getModsXpath()->query('/mods:mods/mods:relatedItem[@type="original"]/mods:titleInfo/mods:title');
191
        if ($node->length > 0) {
192
            return $node->item(0)->nodeValue;
193
        }
194
195
        return null;
196
    }
197
198
    public function getDateIssued()
199
    {
200
201
        $dateIssued = $this->getModsXpath()->query('/mods:mods/mods:originInfo[@eventType="distribution"]/mods:dateIssued[@encoding="iso8601"][@keyDate="yes"]');
202
        if ($dateIssued->length > 0) {
203
            return $dateIssued->item(0)->nodeValue;
204
        }
205
206
        return null;
207
    }
208
209
    public function removeDateIssued()
210
    {
211
212
        $dateIssued = $this->getModsXpath()->query('/mods:mods/mods:originInfo[@eventType="distribution"]/mods:dateIssued[@encoding="iso8601"][@keyDate="yes"]');
213
        if ($dateIssued->length > 0) {
214
            $dateIssued->item(0)->parentNode->removeChild($dateIssued->item(0));
215
        }
216
217
    }
218
219
    public function hasQucosaUrn()
220
    {
221
        $urnNodeList = $this->getModsXpath()->query('/mods:mods/mods:identifier[@type="qucosa:urn"]');
222
223
        $hasUrn = false;
224
225
        foreach ($urnNodeList as $urnNode) {
226
            $value  = $urnNode->nodeValue;
227
            $hasUrn = $hasUrn || !empty($value);
228
        }
229
230
        return $hasUrn;
231
    }
232
233
    public function getQucosaUrn()
234
    {
235
        $urnNodeList = $this->getModsXpath()->query('/mods:mods/mods:identifier[@type="qucosa:urn"]');
236
        $urnList     = '';
237
238
        if ($urnNodeList != null) {
239
            foreach ($urnNodeList as $urnNode) {
240
                $urnList = $urnNode->nodeValue;
241
            }
242
        }
243
        return $urnList;
244
    }
245
246
    public function addQucosaUrn($urn)
247
    {
248
        $rootNode = $this->getModsXpath()->query('/mods:mods');
249
250
        if ($rootNode->length == 1) {
251
            $newUrn = $this->modsDom->createElement('mods:identifier');
252
            $newUrn->setAttribute('type', 'qucosa:urn');
253
            $newUrn->nodeValue = $urn;
254
            $rootNode->item(0)->appendChild($newUrn);
255
        } else {
256
            throw new \Exception('Invalid xml data.');
257
        }
258
259
    }
260
261
    public function clearAllUrn()
262
    {
263
        $urnNodeList = $this->getModsXpath()->query('/mods:mods/mods:identifier[@type="urn"]');
264
265
        foreach ($urnNodeList as $urnNode) {
266
            $urnNode->parentNode->removeChild($urnNode);
267
        }
268
269
        $urnNodeList = $this->getModsXpath()->query('/mods:mods/mods:identifier[@type="qucosa:urn"]');
270
        foreach ($urnNodeList as $urnNode) {
271
            $urnNode->parentNode->removeChild($urnNode);
272
        }
273
    }
274
275
    /**
276
     * @return string
277
     */
278
    public function getSourceDetails()
279
    {
280
        $data = [];
281
282
        $dataNodes[] = $this->getModsXpath()->query('/mods:mods/mods:relatedItem[@type="original"]');
0 ignored issues
show
Comprehensibility Best Practice introduced by
$dataNodes was never initialized. Although not strictly required by PHP, it is generally a good practice to add $dataNodes = array(); before regardless.
Loading history...
283
284
        $dataNodes[] = $this->getModsXpath()->query(
285
            '/mods:mods/mods:part[@type="article"]/mods:detail/mods:number'
286
        );
287
288
        $dataNodes[] = $this->getModsXpath()->query('/mods:mods/mods:part[@type="section"]');
289
290
        foreach ($dataNodes as $dataNode) {
291
            foreach ($dataNode as $node) {
292
                if ($node->hasChildNodes()) {
293
                    foreach ($node->childNodes as $n) {
294
                        $data[] = preg_replace('/\s+/', ' ', $n->textContent);
295
                    }
296
                } else {
297
                    $data[] = preg_replace('/\s+/', ' ', $node->textContent);
298
                }
299
            }
300
        }
301
302
        $output = trim(implode(' ', $data));
303
        $output = preg_replace('/\s+/ ', ' ', $output);
304
        return $output;
305
    }
306
}
307