Passed
Pull Request — master (#195)
by
unknown
07:19
created

PublicationIdentifier   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
eloc 17
c 1
b 1
f 1
dl 0
loc 46
rs 10
wmc 11

1 Method

Rating   Name   Duplication   Size   Complexity  
B determineIdentifierType() 0 38 11
1
<?php
2
namespace EWW\Dpf\Services\ImportExternalMetadata;
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 PublicationIdentifier
18
{
19
    /**
20
     * Determines whether the identifier is a DOI, ISBN or PMID.
21
     *
22
     * @param $identifier
23
     * @return null|string
24
     */
25
    static function determineIdentifierType($identifier)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
26
    {
27
        // FIXME: Wild guessing happening here. Determining actually requires to parse identifier and
28
        // check if the given string correpsponds to the actual rules and invalid identifiers must be rejected.
29
30
        // DOI
31
        if (strpos($identifier, '10.') === 0) {
32
            return 'DOI';
33
        }
34
35
        // ISBN
36
        $length = strlen(str_replace(['-', ' '], '', $identifier));
37
38
        if ($length === 13) {
39
            if (strpos($identifier, '978') === 0 || strpos($identifier, '979') === 0) {
40
                return 'ISBN';
41
            }
42
        }
43
44
        if ($length === 10) {
45
            return 'ISBN';
46
        }
47
48
        $length = strlen(trim($identifier));
49
        if ($length === 9) {
50
            if (strpos($identifier, '-') === 4) {
51
                return 'ISSN';
52
            }
53
        }
54
55
        // PMID
56
        if (is_numeric($identifier) && intval($identifier) == $identifier) {
57
            if (strlen($identifier) < 10) {
58
                return 'PMID';
59
            }
60
        }
61
62
        return null;
63
    }
64
}
65