Passed
Pull Request — master (#123)
by
unknown
08:11 queued 03:22
created

Client::getData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 10
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\Logger;
18
use TYPO3\CMS\Core\Log\LogManager;
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21
/**
22
 * VIAF API Client class
23
 *
24
 * @package TYPO3
25
 * @subpackage dlf
26
 *
27
 * @access public
28
 **/
29
class Client
30
{
31
    /**
32
     * @access protected
33
     * @var Logger This holds the logger
34
     */
35
    protected Logger $logger;
36
37
    /**
38
     * @access private
39
     * @var string The VIAF API endpoint
40
     **/
41
    private string $endpoint = 'viaf.xml';
42
43
    /**
44
     * @access private
45
     * @var string The VIAF URL for the profile
46
     **/
47
    private string $viafUrl;
48
49
    /**
50
     * @access private
51
     * @var RequestFactoryInterface The request object
52
     **/
53
    private RequestFactoryInterface $requestFactory;
54
55
    /**
56
     * Constructs a new instance
57
     *
58
     * @access public
59
     *
60
     * @param string $viaf the VIAF identifier of the profile
61
     * @param RequestFactory $requestFactory a request object to inject
62
     *
63
     * @return void
64
     **/
65
    public function __construct(string $viaf, RequestFactory $requestFactory)
66
    {
67
        $this->logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(static::class);
68
        $this->viafUrl = 'http://viaf.org/viaf/' . $viaf;
69
        $this->requestFactory = $requestFactory;
70
    }
71
72
    /**
73
     * Sets API endpoint
74
     *
75
     * @access public
76
     *
77
     * @param string $endpoint the shortname of the endpoint
78
     *
79
     * @return void
80
     */
81
    public function setEndpoint(string $endpoint): void {
82
        $this->endpoint = $endpoint;
83
    }
84
85
    /**
86
     * Get the profile data
87
     *
88
     * @access public
89
     *
90
     * @return object|bool
91
     **/
92
    public function getData()
93
    {
94
        $url = $this->getApiEndpoint();
95
        try {
96
            $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

96
            /** @scrutinizer ignore-call */ 
97
            $response = $this->requestFactory->request($url);
Loading history...
97
        } catch (\Exception $e) {
98
            $this->logger->warning('Could not fetch data from URL "' . $url . '". Error: ' . $e->getMessage() . '.');
99
            return false;
100
        }
101
        return $response->getBody()->getContents();
102
    }
103
104
    /**
105
     * Creates the qualified API endpoint for retrieving the desired data
106
     * 
107
     * @access private
108
     *
109
     * @return string
110
     **/
111
    private function getApiEndpoint(): string
112
    {
113
        return $this->viafUrl . '/' . $this->endpoint;
114
    }
115
}
116