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); |
|
|
|
|
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
|
|
|
|