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

Client::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
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 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
     * @param string  $endpoint the shortname of the endpoint
88
     */
89
    public function setEndpoint($endpoint) {
90
        $this->endpoint = $endpoint;
91
    }
92
93
    /**
94
     * Get the profile data
95
     *
96
     * @return object|bool
97
     **/
98
    public function getData()
99
    {
100
        $url = $this->getApiEndpoint();
101
        try {
102
            $response = $this->requestFactory->request($url);
103
        } catch (\Exception $e) {
104
            $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

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