Passed
Pull Request — master (#123)
by
unknown
10:39 queued 05:50
created

Profile::getRaw()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
5
 *
6
 * This file is part of the Kitodo and TYPO3 projects.
7
 *
8
 * @license GNU General Public License version 3 or later.
9
 * For the full copyright and license information, please read the
10
 * LICENSE.txt file that was distributed with this source code.
11
 */
12
13
namespace Kitodo\Dlf\Api\Orcid;
14
15
use Kitodo\Dlf\Common\Helper;
16
use TYPO3\CMS\Core\Http\RequestFactory;
17
use TYPO3\CMS\Core\Log\Logger;
18
use TYPO3\CMS\Core\Log\LogManager;
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21
/**
22
 * ORCID API Profile class
23
 *
24
 * @package TYPO3
25
 * @subpackage dlf
26
 *
27
 * @access public
28
 **/
29
class Profile
30
{
31
    /**
32
     * @access protected
33
     * @var Logger This holds the logger
34
     */
35
    protected Logger $logger;
36
37
    /**
38
     * @access private
39
     * @var Client This holds the client
40
     */
41
    private Client $client;
42
43
    /**
44
     * @access private
45
     * @var \SimpleXmlElement|false The raw ORCID profile
46
     **/
47
    private $raw = false;
48
49
    /**
50
     * Constructs client instance
51
     *
52
     * @access public
53
     *
54
     * @param string $orcid: the ORCID to search for
55
     *
56
     * @return void
57
     **/
58
    public function __construct(string $orcid)
59
    {
60
        $this->logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(static::class);
61
        $this->client = new Client($orcid, GeneralUtility::makeInstance(RequestFactory::class));
62
    }
63
64
    /**
65
     * Get the ORCID profile data
66
     *
67
     * @access public
68
     *
69
     * @return array|false
70
     **/
71
    public function getData()
72
    {
73
        $this->getRaw('person');
74
        if ($this->raw !== false && !empty($this->raw)) {
75
            $data = [];
76
            $data['address'] = $this->getAddress();
77
            $data['email'] = $this->getEmail();
78
            $data['fullName'] = $this->getFullName();
79
            return $data;
80
        } else {
81
            $this->logger->warning('No data found for given ORCID');
82
            return false;
83
        }
84
    }
85
86
    /**
87
     * Get the address
88
     *
89
     * @access public
90
     *
91
     * @return string|false
92
     **/
93
    public function getAddress()
94
    {
95
        $this->getRaw('address');
96
        if ($this->raw !== false && !empty($this->raw)) {
97
            $this->raw->registerXPathNamespace('address', 'http://www.orcid.org/ns/address');
98
            return (string) $this->raw->xpath('./address:address/address:country')[0];
99
        } else {
100
            $this->logger->warning('No address found for given ORCID');
101
            return false;
102
        }
103
    }
104
105
    /**
106
     * Get the email
107
     *
108
     * @access public
109
     *
110
     * @return string|false
111
     **/
112
    public function getEmail()
113
    {
114
        $this->getRaw('email');
115
        if ($this->raw !== false && !empty($this->raw)) {
116
            $this->raw->registerXPathNamespace('email', 'http://www.orcid.org/ns/email');
117
            return (string) $this->raw->xpath('./email:email/email:email')[0];
118
        } else {
119
            $this->logger->warning('No email found for given ORCID');
120
            return false;
121
        }
122
    }
123
124
    /**
125
     * Get the full name
126
     *
127
     * @access public
128
     *
129
     * @return string|false
130
     **/
131
    public function getFullName()
132
    {
133
        $this->getRaw('personal-details');
134
        if ($this->raw !== false && !empty($this->raw)) {
135
            $this->raw->registerXPathNamespace('personal-details', 'http://www.orcid.org/ns/personal-details');
136
            $givenNames = $this->raw->xpath('./personal-details:name/personal-details:given-names');
137
            $familyName = $this->raw->xpath('./personal-details:name/personal-details:family-name');
138
            return (string) $givenNames[0] . ' ' . (string) $familyName[0];
139
        } else {
140
            $this->logger->warning('No name found for given ORCID');
141
            return false;
142
        }
143
    }
144
145
    /**
146
     * Get the ORCID part of profile data for given endpoint
147
     *
148
     * @access private
149
     *
150
     * @param string $endpoint the shortname of the endpoint
151
     *
152
     * @return void
153
     **/
154
    private function getRaw(string $endpoint): void
155
    {
156
        $this->client->setEndpoint($endpoint);
157
        $data = $this->client->getData();
158
        if ($data != false) {
159
            $this->raw = Helper::getXmlFileAsString($data);
160
        }
161
    }
162
}
163