Passed
Pull Request — master (#123)
by
unknown
04:15
created

Client::setEndpoint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
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 Psr\Http\Message\RequestFactoryInterface;
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 Client class
23
 *
24
 * @package TYPO3
25
 * @subpackage dlf
26
 *
27
 * @access public
28
 **/
29
class Client
30
{
31
    /**
32
     * @var string constant for API hostname
33
     **/
34
    const HOSTNAME = 'orcid.org';
35
36
    /**
37
     * @var string constant for API version
38
     **/
39
    const VERSION = '3.0';
40
41
    /**
42
     * @access protected
43
     * @var Logger This holds the logger
44
     */
45
    protected Logger $logger;
46
47
    /**
48
     * @access private
49
     * @var string The ORCID API endpoint
50
     **/
51
    private string $endpoint = 'record';
52
53
    /**
54
     * @access private
55
     * @var string The ORCID API access level
56
     **/
57
    private string $level = 'pub';
58
59
    /**
60
     * @access private
61
     * @var string The ORCID ID to search for
62
     **/
63
    private string $orcid;
64
65
    /**
66
     * @access private
67
     * @var RequestFactoryInterface The request object
68
     **/
69
    private RequestFactoryInterface $requestFactory;
70
71
    /**
72
     * Constructs a new instance
73
     *
74
     * @access public
75
     *
76
     * @param string $orcid the ORCID to search for
77
     * @param RequestFactory $requestFactory a request object to inject
78
     *
79
     * @return void
80
     **/
81
    public function __construct(string $orcid, RequestFactory $requestFactory)
82
    {
83
        $this->logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(static::class);
84
        $this->orcid = $orcid;
85
        $this->requestFactory = $requestFactory;
86
    }
87
88
    /**
89
     * Sets API endpoint
90
     *
91
     * @access public
92
     *
93
     * @param string $endpoint the shortname of the endpoint
94
     *
95
     * @return void
96
     */
97
    public function setEndpoint(string $endpoint): void {
98
        $this->endpoint = $endpoint;
99
    }
100
101
    /**
102
     * Get the profile data
103
     *
104
     * @access public
105
     *
106
     * @return object|bool
107
     **/
108
    public function getData()
109
    {
110
        $url = $this->getApiEndpoint();
111
        try {
112
            $response = $this->requestFactory->request($url);
0 ignored issues
show
Bug introduced by
The method request() does not exist on Psr\Http\Message\RequestFactoryInterface. It seems like you code against a sub-type of Psr\Http\Message\RequestFactoryInterface such as TYPO3\CMS\Core\Http\RequestFactory. ( Ignorable by Annotation )

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

112
            /** @scrutinizer ignore-call */ 
113
            $response = $this->requestFactory->request($url);
Loading history...
113
        } catch (\Exception $e) {
114
            $this->logger->warning('Could not fetch data from URL "' . $url . '". Error: ' . $e->getMessage() . '.');
115
            return false;
116
        }
117
        return $response->getBody()->getContents();
118
    }
119
120
    /**
121
     * Creates the qualified API endpoint for retrieving the desired data
122
     *
123
     * @access private
124
     *
125
     * @return string
126
     **/
127
    private function getApiEndpoint(): string
128
    {
129
        $url  = 'https://' . $this->level . '.' . self::HOSTNAME;
130
        $url .= '/v' . self::VERSION . '/';
131
        $url .= $this->orcid;
132
        $url .= '/' . $this->endpoint;
133
        return $url;
134
    }
135
}
136