We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| 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 | { |
||
| 83 | $this->endpoint = $endpoint; |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Get the profile data |
||
| 88 | * |
||
| 89 | * @access public |
||
| 90 | * |
||
| 91 | * @return object|bool |
||
| 92 | **/ |
||
| 93 | public function getData() |
||
| 94 | { |
||
| 95 | $url = $this->getApiEndpoint(); |
||
| 96 | try { |
||
| 97 | $response = $this->requestFactory->request($url); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 98 | } catch (\Exception $e) { |
||
| 99 | $this->logger->warning('Could not fetch data from URL "' . $url . '". Error: ' . $e->getMessage() . '.'); |
||
| 100 | return false; |
||
| 101 | } |
||
| 102 | return $response->getBody()->getContents(); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Creates the qualified API endpoint for retrieving the desired data |
||
| 107 | * |
||
| 108 | * @access private |
||
| 109 | * |
||
| 110 | * @return string |
||
| 111 | **/ |
||
| 112 | private function getApiEndpoint(): string |
||
| 113 | { |
||
| 114 | return $this->viafUrl . '/' . $this->endpoint; |
||
| 115 | } |
||
| 116 | } |
||
| 117 |