Passed
Pull Request — master (#123)
by Sebastian
04:18
created

Client::setEndpoint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 2
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 Psr\Http\Message\RequestFactoryInterface;
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 Client class
22
 *
23
 * @author Beatrycze Volk <[email protected]>
24
 * @package TYPO3
25
 * @subpackage dlf
26
 * @access public
27
 **/
28
class Client
29
{
30
    /**
31
     * constants for API endpoint
32
     **/
33
    const HOSTNAME  = 'orcid.org';
34
    const VERSION   = '3.0';
35
36
    /**
37
     * This holds the logger
38
     *
39
     * @var LogManager
40
     * @access protected
41
     */
42
    protected $logger;
43
44
    /**
45
     * The ORCID API endpoint
46
     *
47
     * @var string
48
     **/
49
    private $endpoint = 'record';
50
51
    /**
52
     * The ORCID API access level
53
     *
54
     * @var string
55
     **/
56
    private $level = 'pub';
57
58
    /**
59
     * The ORCID ID to search for
60
     *
61
     * @var string
62
     **/
63
    private $orcid = null;
64
65
    /**
66
     * The request object
67
     *
68
     * @var RequestFactoryInterface
69
     **/
70
    private $requestFactory = null;
71
72
    /**
73
     * Constructs a new instance
74
     *
75
     * @param string $orcid: the ORCID to search for
76
     * @param RequestFactory $requestFactory a request object to inject
77
     * @return void
78
     **/
79
    public function __construct($orcid, RequestFactory $requestFactory)
80
    {
81
        $this->logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(static::class);
82
        $this->orcid = $orcid;
83
        $this->requestFactory = $requestFactory;
84
    }
85
86
    /**
87
     * Sets API endpoint
88
     *
89
     * @param string  $endpoint the shortname of the endpoint
90
     *
91
     * @return void
92
     */
93
    public function setEndpoint($endpoint) {
94
        $this->endpoint = $endpoint;
95
    }
96
97
    /**
98
     * Get the profile data
99
     *
100
     * @return object|bool
101
     **/
102
    public function getData()
103
    {
104
        $url = $this->getApiEndpoint();
105
        try {
106
            $response = $this->requestFactory->request($url);
107
        } catch (\Exception $e) {
108
            $this->logger->warning('Could not fetch data from URL "' . $url . '". Error: ' . $e->getMessage() . '.');
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

108
            $this->logger->/** @scrutinizer ignore-call */ 
109
                           warning('Could not fetch data from URL "' . $url . '". Error: ' . $e->getMessage() . '.');

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...
109
            return false;
110
        }
111
        return $response->getBody()->getContents();
112
    }
113
114
    /**
115
     * Creates the qualified API endpoint for retrieving the desired data
116
     *
117
     * @return string
118
     **/
119
    private function getApiEndpoint()
120
    {
121
        $url  = 'https://' . $this->level . '.' . self::HOSTNAME;
122
        $url .= '/v' . self::VERSION . '/';
123
        $url .= $this->orcid;
124
        $url .= '/' . $this->endpoint;
125
        return $url;
126
    }
127
}
128