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

Client::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
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\Viaf;
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
 * VIAF 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
     * This holds the logger
32
     *
33
     * @var LogManager
34
     * @access protected
35
     */
36
    protected $logger;
37
38
    /**
39
     * The ORCID API endpoint
40
     *
41
     * @var string
42
     **/
43
    private $endpoint = 'viaf.xml';
44
45
    /**
46
     * The VIAF URL for the profile
47
     *
48
     * @var string
49
     **/
50
    private $viafUrl = null;
51
52
    /**
53
     * The request object
54
     *
55
     * @var RequestFactoryInterface
56
     **/
57
    private $requestFactory = null;
58
59
    /**
60
     * Constructs a new instance
61
     *
62
     * @param string $viaf: the VIAF identifier of the profile
63
     * @param RequestFactory $requestFactory a request object to inject
64
     * @return void
65
     **/
66
    public function __construct($viaf, RequestFactory $requestFactory)
67
    {
68
        $this->logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(static::class);
69
        $this->viafUrl = 'http://viaf.org/viaf/' . $viaf;
70
        $this->requestFactory = $requestFactory;
71
    }
72
73
    /**
74
     * Sets API endpoint
75
     *
76
     * @param string  $endpoint the shortname of the endpoint
77
     *
78
     * @return void
79
     */
80
    public function setEndpoint($endpoint) {
81
        $this->endpoint = $endpoint;
82
    }
83
84
    /**
85
     * Get the profile data
86
     *
87
     * @return object|bool
88
     **/
89
    public function getData()
90
    {
91
        $url = $this->getApiEndpoint();
92
        try {
93
            $response = $this->requestFactory->request($url);
94
        } catch (\Exception $e) {
95
            $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

95
            $this->logger->/** @scrutinizer ignore-call */ 
96
                           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...
96
            return false;
97
        }
98
        return $response->getBody()->getContents();
99
    }
100
101
    /**
102
     * Creates the qualified API endpoint for retrieving the desired data
103
     *
104
     * @return string
105
     **/
106
    protected function getApiEndpoint()
107
    {
108
        return $this->viafUrl . '/' . $this->endpoint;
109
    }
110
}
111