Passed
Pull Request — master (#195)
by
unknown
06:45
created

DataCiteImporter   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 44
c 1
b 0
f 0
dl 0
loc 115
rs 10
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getImporterName() 0 3 1
A findByIdentifier() 0 32 5
A types() 0 18 1
A search() 0 3 1
A getDefaultXsltTransformation() 0 7 1
A getDefaultXsltFilePath() 0 4 1
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
\Httpful\Bootstrap::init();
18
19
use \Httpful\Request;
20
use Symfony\Component\Serializer\Encoder\XmlEncoder;
21
use EWW\Dpf\Domain\Model\DataCiteMetadata;
22
use EWW\Dpf\Domain\Model\ExternalMetadata;
23
24
class DataCiteImporter extends AbstractImporter implements Importer
25
{
26
    /**
27
     * @var string
28
     */
29
    protected $apiUrl = "https://api.datacite.org";
30
31
    /**
32
     * @var string
33
     */
34
    protected $resource = "/dois";
35
36
37
    /**
38
     * Returns the list of all publication types
39
     *
40
     * @return array
41
     */
42
    public static function types()
43
    {
44
        return [
45
            'Audiovisual',
46
            'Collection',
47
            'DataPaper',
48
            'Dataset',
49
            'Event',
50
            'Image',
51
            'InteractiveResource',
52
            'Model',
53
            'PhysicalObject',
54
            'Service',
55
            'Software',
56
            'Sound',
57
            'Text',
58
            'Workflow',
59
            'Other'
60
        ];
61
    }
62
63
    /**
64
     * @param string $identifier
65
     * @return ExternalMetadata|null
66
     */
67
    public function findByIdentifier($identifier)
68
    {
69
        try {
70
            $response = Request::get($this->apiUrl . $this->resource . "/".$identifier)->send();
71
72
            if (!$response->hasErrors() && $response->code == 200) {
73
                $jsonString = $response->__toString();
74
                if ($jsonString) {
75
                    $data = json_decode($response->__toString(),true);
76
                    $encoder = new XmlEncoder();
77
78
                    /** @var DataCiteMetadata $dataCiteMetadata */
79
                    $dataCiteMetadata = $this->objectManager->get(DataCiteMetadata::class);
80
81
                    $dataCiteMetadata->setSource(get_class($this));
82
                    $dataCiteMetadata->setFeUser($this->security->getUser()->getUid());
83
                    $dataCiteMetadata->setData($encoder->encode($data, 'xml'));
84
                    $dataCiteMetadata->setPublicationIdentifier($identifier);
85
86
                    return $dataCiteMetadata;
87
                }
88
89
            } else {
90
                return null;
91
            }
92
93
        } catch (\Throwable $throwable) {
94
            $this->logger->error($throwable->getMessage());
95
            throw $throwable;
96
        }
97
98
        return null;
99
    }
100
101
    /**
102
     * @param string $query
103
     * @return mixed
104
     */
105
    public function search($query)
106
    {
107
        return null;
108
    }
109
110
111
    /**
112
     * @return \EWW\Dpf\Domain\Model\TransformationFile
113
     */
114
    protected function getDefaultXsltTransformation() : ?\EWW\Dpf\Domain\Model\TransformationFile
115
    {
116
        /** @var \EWW\Dpf\Domain\Model\Client $client */
117
        $client = $this->clientRepository->findAll()->current();
118
119
        /** @var \EWW\Dpf\Domain\Model\TransformationFile $xsltTransformationFile */
120
        return $client->getDataciteTransformation()->current();
121
    }
122
123
    /**
124
     * @return string
125
     */
126
    protected function getDefaultXsltFilePath() : string
127
    {
128
        return \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName(
129
            'EXT:dpf/Resources/Private/Xslt/datacite-default.xsl'
130
        );
131
    }
132
133
    /**
134
     * @return string
135
     */
136
    protected function getImporterName()
137
    {
138
        return 'datacite';
139
    }
140
141
142
}
143