Passed
Pull Request — master (#123)
by Sebastian
03:15
created

Profile::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
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\LogManager;
18
use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20
/**
21
 * ORCID API Profile class
22
 *
23
* @author Beatrycze Volk <[email protected]>
24
 * @package TYPO3
25
 * @subpackage dlf
26
 * @access public
27
 **/
28
class Profile
29
{
30
    /**
31
     * This holds the logger
32
     *
33
     * @var LogManager
34
     * @access protected
35
     */
36
    protected $logger;
37
38
     /**
39
     * This holds the client
40
     *
41
     * @var Client
42
     * @access protected
43
     */
44
    protected $client;
45
46
    /**
47
     * The raw ORCID profile
48
     *
49
     * @var SimpleXmlElement
0 ignored issues
show
Bug introduced by
The type Kitodo\Dlf\Api\Orcid\SimpleXmlElement was not found. Did you mean SimpleXmlElement? If so, make sure to prefix the type with \.
Loading history...
50
     **/
51
    private $raw = null;
52
53
    /**
54
     * Constructs client instance
55
     *
56
     * @param string $orcid: the ORCID to search for
57
     *
58
     * @return void
59
     **/
60
    public function __construct($orcid)
61
    {
62
        $this->logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(static::class);
63
        $this->client = new Client($orcid, GeneralUtility::makeInstance(RequestFactory::class));
64
    }
65
66
    /**
67
     * Get the ORCID profile data
68
     *
69
     * @return array|bool
70
     **/
71
    public function getData()
72
    {
73
        $this->getRaw('person');
74
        if (!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');
0 ignored issues
show
Bug introduced by
The method warning() does not exist on TYPO3\CMS\Core\Log\LogManager. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
            $this->logger->/** @scrutinizer ignore-call */ 
82
                           warning('No data found for given ORCID');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
82
            return false;
83
        }
84
    }
85
86
    /**
87
     * Get the address
88
     *
89
     * @return string|bool
90
     **/
91
    public function getAddress()
92
    {
93
        $this->getRaw('address');
94
        if (!empty($this->raw)) {
95
            $this->raw->registerXPathNamespace('address', 'http://www.orcid.org/ns/address');
96
            return (string) $this->raw->xpath('./address:address/address:country')[0];
97
        } else {
98
            $this->logger->warning('No address found for given ORCID');
99
            return false;
100
        }
101
    }
102
103
    /**
104
     * Get the email
105
     *
106
     * @return string|bool
107
     **/
108
    public function getEmail()
109
    {
110
        $this->getRaw('email');
111
        if (!empty($this->raw)) {
112
            $this->raw->registerXPathNamespace('email', 'http://www.orcid.org/ns/email');
113
            return (string) $this->raw->xpath('./email:email/email:email')[0];
114
        } else {
115
            $this->logger->warning('No email found for given ORCID');
116
            return false;
117
        }
118
    }
119
120
    /**
121
     * Get the full name
122
     *
123
     * @return string|bool
124
     **/
125
    public function getFullName()
126
    {
127
        $this->getRaw('personal-details');
128
        if (!empty($this->raw)) {
129
            $this->raw->registerXPathNamespace('personal-details', 'http://www.orcid.org/ns/personal-details');
130
            $givenNames = $this->raw->xpath('./personal-details:name/personal-details:given-names');
131
            $familyName = $this->raw->xpath('./personal-details:name/personal-details:family-name');
132
            return (string) $givenNames[0] . ' ' . (string) $familyName[0];
133
        } else {
134
            $this->logger->warning('No name found for given ORCID');
135
            return false;
136
        }
137
    }
138
139
    /**
140
     * Get the ORCID part of profile data for given endpoint
141
     *
142
     * @param string $endpoint: the endpoint for search URL
143
     *
144
     * @return void
145
     **/
146
    protected function getRaw($endpoint)
147
    {
148
        $this->client->setEndpoint($endpoint);
149
        $data = $this->client->getData();
150
        if (!isset($this->raw) && $data != false) {
151
            $this->raw = Helper::getXmlFileAsString($data);
0 ignored issues
show
Documentation Bug introduced by
It seems like Kitodo\Dlf\Common\Helper...tXmlFileAsString($data) of type false is incompatible with the declared type Kitodo\Dlf\Api\Orcid\SimpleXmlElement of property $raw.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
152
        }
153
    }
154
}
155