feat: adopt myep api client to form finisher
This commit is contained in:
@@ -5,6 +5,7 @@ namespace EP\EpProducts\MyEP;
|
|||||||
use League\OAuth2\Client\Provider\AbstractProvider;
|
use League\OAuth2\Client\Provider\AbstractProvider;
|
||||||
use League\OAuth2\Client\Provider\GenericProvider;
|
use League\OAuth2\Client\Provider\GenericProvider;
|
||||||
use League\OAuth2\Client\Token\AccessToken;
|
use League\OAuth2\Client\Token\AccessToken;
|
||||||
|
use Psr\Log\LoggerAwareTrait;
|
||||||
use Symfony\Component\HttpClient\HttpClient;
|
use Symfony\Component\HttpClient\HttpClient;
|
||||||
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
||||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||||
@@ -13,6 +14,8 @@ use TYPO3\CMS\Core\Utility\GeneralUtility;
|
|||||||
|
|
||||||
class ApiClient
|
class ApiClient
|
||||||
{
|
{
|
||||||
|
use LoggerAwareTrait;
|
||||||
|
|
||||||
private static ?AccessToken $accessToken = null;
|
private static ?AccessToken $accessToken = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -25,6 +28,7 @@ class ApiClient
|
|||||||
try {
|
try {
|
||||||
$response = $httpClient->request('GET', 'last-update');
|
$response = $httpClient->request('GET', 'last-update');
|
||||||
} catch (TransportExceptionInterface $e) {
|
} catch (TransportExceptionInterface $e) {
|
||||||
|
$this->logger->error($e->getMessage());
|
||||||
throw new ApiException($e->getMessage());
|
throw new ApiException($e->getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,6 +45,7 @@ class ApiClient
|
|||||||
|
|
||||||
return new \DateTimeImmutable($data['timestamp']);
|
return new \DateTimeImmutable($data['timestamp']);
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
|
$this->logger->error($e->getMessage());
|
||||||
throw new ApiException($e->getMessage());
|
throw new ApiException($e->getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -55,6 +60,7 @@ class ApiClient
|
|||||||
try {
|
try {
|
||||||
$response = $httpClient->request('GET', 'pickups');
|
$response = $httpClient->request('GET', 'pickups');
|
||||||
} catch (TransportExceptionInterface $e) {
|
} catch (TransportExceptionInterface $e) {
|
||||||
|
$this->logger->error($e->getMessage());
|
||||||
throw new ApiException($e->getMessage());
|
throw new ApiException($e->getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,6 +71,7 @@ class ApiClient
|
|||||||
|
|
||||||
return $response->toArray(false);
|
return $response->toArray(false);
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
|
$this->logger->error($e->getMessage());
|
||||||
throw new ApiException($e->getMessage());
|
throw new ApiException($e->getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -79,6 +86,7 @@ class ApiClient
|
|||||||
try {
|
try {
|
||||||
$response = $httpClient->request('GET', 'travels');
|
$response = $httpClient->request('GET', 'travels');
|
||||||
} catch (TransportExceptionInterface $e) {
|
} catch (TransportExceptionInterface $e) {
|
||||||
|
$this->logger->error($e->getMessage());
|
||||||
throw new ApiException($e->getMessage());
|
throw new ApiException($e->getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,6 +97,24 @@ class ApiClient
|
|||||||
|
|
||||||
return $response->toArray(false);
|
return $response->toArray(false);
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
|
$this->logger->error($e->getMessage());
|
||||||
|
throw new ApiException($e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws ApiException
|
||||||
|
*/
|
||||||
|
public function registerAddress(array $addressData): void
|
||||||
|
{
|
||||||
|
$httpClient = $this->getHttpClient();
|
||||||
|
|
||||||
|
try {
|
||||||
|
$httpClient->request('POST', 'contactform', [
|
||||||
|
'json' => $addressData,
|
||||||
|
]);
|
||||||
|
} catch (TransportExceptionInterface $e) {
|
||||||
|
$this->logger->error($e->getMessage());
|
||||||
throw new ApiException($e->getMessage());
|
throw new ApiException($e->getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,48 +1,24 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace EP\EpTheme\Form;
|
namespace EP\EpTheme\Form;
|
||||||
|
|
||||||
/***************************************************************
|
use EP\EpProducts\MyEP\ApiClient;
|
||||||
*
|
|
||||||
* Copyright notice
|
|
||||||
*
|
|
||||||
* (c) 2019 Björn Fromme <[email protected]>, dreipunktnull
|
|
||||||
*
|
|
||||||
* All rights reserved
|
|
||||||
*
|
|
||||||
* This script is part of the TYPO3 project. The TYPO3 project is
|
|
||||||
* free software; you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation; either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* The GNU General Public License can be found at
|
|
||||||
* http://www.gnu.org/copyleft/gpl.html.
|
|
||||||
*
|
|
||||||
* This script is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* This copyright notice MUST APPEAR in all copies of the script!
|
|
||||||
***************************************************************/
|
|
||||||
|
|
||||||
use EP\EpTheme\Service\BookingApiService;
|
|
||||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||||
use TYPO3\CMS\Form\Domain\Finishers\AbstractFinisher;
|
use TYPO3\CMS\Form\Domain\Finishers\AbstractFinisher;
|
||||||
|
|
||||||
class BpnApiFinisher extends AbstractFinisher
|
class BpnApiFinisher extends AbstractFinisher
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var BookingApiService
|
* @var ApiClient
|
||||||
*/
|
*/
|
||||||
protected $bookingApiService;
|
protected $apiClient;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param BookingApiService $bookingApiService
|
* @param ApiClient $apiClient
|
||||||
*/
|
*/
|
||||||
public function injectBookingApiService(BookingApiService $bookingApiService)
|
public function injectApiClient(ApiClient $apiClient)
|
||||||
{
|
{
|
||||||
$this->bookingApiService = $bookingApiService;
|
$this->apiClient = $apiClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function executeInternal()
|
protected function executeInternal()
|
||||||
@@ -56,7 +32,7 @@ class BpnApiFinisher extends AbstractFinisher
|
|||||||
$values[$remote] = $formValues[$local];
|
$values[$remote] = $formValues[$local];
|
||||||
}
|
}
|
||||||
if (count($values) > 0) {
|
if (count($values) > 0) {
|
||||||
$this->bookingApiService->registerAddress($values);
|
$this->apiClient->registerAddress($values);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,91 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace EP\EpTheme\Service;
|
|
||||||
|
|
||||||
/***************************************************************
|
|
||||||
*
|
|
||||||
* Copyright notice
|
|
||||||
*
|
|
||||||
* (c) 2019 Björn Fromme <[email protected]>, dreipunktnull
|
|
||||||
*
|
|
||||||
* All rights reserved
|
|
||||||
*
|
|
||||||
* This script is part of the TYPO3 project. The TYPO3 project is
|
|
||||||
* free software; you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation; either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* The GNU General Public License can be found at
|
|
||||||
* http://www.gnu.org/copyleft/gpl.html.
|
|
||||||
*
|
|
||||||
* This script is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* This copyright notice MUST APPEAR in all copies of the script!
|
|
||||||
***************************************************************/
|
|
||||||
|
|
||||||
use Psr\Log\LoggerAwareInterface;
|
|
||||||
use Psr\Log\LoggerAwareTrait;
|
|
||||||
use Symfony\Component\HttpClient\HttpClient;
|
|
||||||
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
|
||||||
use TYPO3\CMS\Core\SingletonInterface;
|
|
||||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
|
||||||
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
|
|
||||||
|
|
||||||
class BookingApiService implements SingletonInterface, LoggerAwareInterface
|
|
||||||
{
|
|
||||||
use LoggerAwareTrait;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
protected $apiToken;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
protected $apiEndpointUrl;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var int
|
|
||||||
*/
|
|
||||||
protected $apiTimeout;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param ConfigurationManagerInterface $manager
|
|
||||||
*/
|
|
||||||
public function __construct(ConfigurationManagerInterface $manager)
|
|
||||||
{
|
|
||||||
$settings = GeneralUtility::removeDotsFromTS(
|
|
||||||
$manager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT)
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->apiToken = $settings['plugin']['tx_eptheme']['settings']['myEpApiToken'];
|
|
||||||
$this->apiEndpointUrl = $settings['plugin']['tx_eptheme']['settings']['myEpApiEndpointUrl'];
|
|
||||||
$this->apiTimeout = $settings['plugin']['tx_eptheme']['settings']['myEpApiTimeout'];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $addressData
|
|
||||||
*/
|
|
||||||
public function registerAddress(array $addressData)
|
|
||||||
{
|
|
||||||
$client = HttpClient::create();
|
|
||||||
|
|
||||||
try {
|
|
||||||
$client->request('POST', $this->apiEndpointUrl . '/api/contactform', [
|
|
||||||
'headers' => [
|
|
||||||
'X-AUTH-TOKEN' => $this->apiToken,
|
|
||||||
'Content-type' => 'application/json',
|
|
||||||
],
|
|
||||||
'timeout' => $this->apiTimeout,
|
|
||||||
'body' => json_encode($addressData),
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
catch (TransportExceptionInterface $e) {
|
|
||||||
$this->logger->error($e->getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user