Finalize contact form handling via API

This commit is contained in:
Björn Fromme
2019-10-01 17:17:10 +02:00
parent 2bcc3108e4
commit f193fa9419
4 changed files with 48 additions and 21 deletions
@@ -3,6 +3,8 @@
namespace EP\EpTheme\Form;
use EP\EpTheme\Service\BookingApiService;
use Symfony\Component\OptionsResolver\Exception\MissingOptionsException;
use Symfony\Component\OptionsResolver\OptionsResolver;
use TYPO3\CMS\Form\Domain\Finishers\AbstractFinisher;
class BpnApiFinisher extends AbstractFinisher
@@ -26,16 +28,26 @@ class BpnApiFinisher extends AbstractFinisher
protected function executeInternal()
{
$formValues = $this->finisherContext->getFormValues();
$optionsResolver = new OptionsResolver();
$optionsResolver->setRequired(['lastName', 'firstName', 'gender', 'email']);
$optionsResolver->setDefined([
'lastName', 'firstName', 'gender', 'email', 'street', 'zipCode', 'city', 'phone'
]);
$addressData = [
'lastName' => $formValues['name'],
'firstName' => $formValues['vorname'],
'gender' => $formValues['geschlecht'],
'firstName' => $formValues['firstName'],
'gender' => $formValues['gender'],
'email' => $formValues['email'],
'street' => $formValues['strasse'],
'zipCode' => $formValues['plz'],
'city' => $formValues['ort'],
'phone' => $formValues['telefon'],
'street' => $formValues['street'],
'zipCode' => $formValues['zipCode'],
'city' => $formValues['city'],
'phone' => $formValues['phone'],
];
$this->bookingApiService->registerAddress($addressData);
try {
$sanitizedFormData = $optionsResolver->resolve($addressData);
$this->bookingApiService->registerAddress($sanitizedFormData);
}
catch (MissingOptionsException $e) {
}
}
}
@@ -2,15 +2,18 @@
namespace EP\EpTheme\Service;
use Symfony\Component\HttpClient\Exception\ServerException;
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
class BookingApiService implements SingletonInterface, LoggerAwareInterface
{
use LoggerAwareTrait;
/**
* @var string
*/
@@ -21,6 +24,11 @@ class BookingApiService implements SingletonInterface
*/
protected $apiEndpointUrl;
/**
* @var int
*/
protected $apiTimeout;
/**
* @param ConfigurationManagerInterface $manager
*/
@@ -31,27 +39,31 @@ class BookingApiService implements SingletonInterface
);
$this->apiToken = $settings['plugin']['tx_eptheme']['settings']['myEpApiToken'];
$this->apiEndpointUrl = $settings['plugin']['tx_eptheme']['settings']['myEpApiBaseUrl'];
$this->apiEndpointUrl = $settings['plugin']['tx_eptheme']['settings']['myEpApiEndpointUrl'];
$this->apiTimeout = $settings['plugin']['tx_eptheme']['settings']['myEpApiTimeout'];
}
/**
* @param array $addressData
* @throws TransportExceptionInterface
*/
public function registerAddress(array $addressData)
{
$client = HttpClient::create();
try {
$client->request('POST', $this->apiEndpointUrl . '/booking/address', [
$client->request('POST', $this->apiEndpointUrl, [
'headers' => [
'X-AUTH-TOKEN' => $this->apiToken,
'Content-type' => 'application/json',
],
'body' => $addressData,
'timeout' => $this->apiTimeout,
'body' => json_encode($addressData),
]);
}
catch (TransportExceptionInterface $e)
{}
catch (ServerException $e)
{}
catch (\Exception $e)
{
$this->logger->error($e->getMessage());
}
}
}
@@ -156,10 +156,12 @@ plugin.tx_eptheme {
# cat=eptheme.events; type=string; label=Partner ID selection label
partnerIdSelectLabel =
# cat=eptheme/900/100; type=string; label=MyE&P API base url
myEpApiBaseUrl =
# cat=eptheme/900/110; type=string; label=MyE&P API auth token
myEpApiToken =
# cat=eptheme/900/100; type=string; label=MyE&P API endpoint url for contactforms
myEpApiEndpointUrl = https://myep.burn.dpn/api/contactform
# cat=eptheme/900/110; type=string; label=MyE&P API timeout in seconds
myEpApiTimeout = 10
# cat=eptheme/900/120; type=string; label=MyE&P API auth token
myEpApiToken = thisisnotsecretsochangeit
}
}
@@ -87,8 +87,9 @@ plugin.tx_eptheme {
germanPageUid = {$plugin.tx_eptheme.settings.germanPageUid}
conceptsPageUid = {$plugin.tx_eptheme.settings.conceptsPageUid}
myEpPageUid = {$plugin.tx_eptheme.settings.myEpPageUid}
myEpApiBaseUrl = {$plugin.tx_eptheme.settings.myEpApiBaseUrl}
myEpApiEndpointUrl = {$plugin.tx_eptheme.settings.myEpApiEndpointUrl}
myEpApiToken = {$plugin.tx_eptheme.settings.myEpApiToken}
myEpApiTimeout = {$plugin.tx_eptheme.settings.myEpApiTimeout}
facebookViewContentValues {
productUid = {$plugin.tx_eptheme.settings.facebookViewContentProductUid}
nameInternal = {$plugin.tx_eptheme.settings.facebookViewContentNameInternal}