173 lines
5.6 KiB
PHP
173 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace App\BusProNet;
|
|
|
|
use App\BusProNet\Model\BaseDataResponse;
|
|
use App\BusProNet\Model\CrmAttributesResponse;
|
|
use App\BusProNet\Model\NotificationResponse;
|
|
use App\BusProNet\Model\ProfileResponse;
|
|
use App\Entity\User;
|
|
use Psr\Log\LoggerInterface;
|
|
use Symfony\Component\OptionsResolver\OptionsResolver;
|
|
use Symfony\Component\Serializer\SerializerInterface;
|
|
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
|
|
|
class ApiClient
|
|
{
|
|
public const TYPE_NOTIFICATION = 'HINWEIS';
|
|
public const TYPE_CUSTOMER_DATA = 'KUNDENKONTO';
|
|
public const TYPE_BASE_DATA_COUNTRIES = 'STAMMLAENDER';
|
|
public const TYPE_BASE_DATA_PICKUPS = 'STAMMZUSTIEGE';
|
|
public const TYPE_BASE_DATA_HOTELS = 'STAMMHOTELS';
|
|
|
|
private array $config;
|
|
|
|
public function __construct(
|
|
private readonly HttpClientInterface $httpClient,
|
|
private readonly SerializerInterface $serializer,
|
|
private readonly ResponseParser $responseParser,
|
|
private readonly LoggerInterface $logger,
|
|
array $options
|
|
) {
|
|
$this->config = $this->resolveOptions($options);
|
|
}
|
|
|
|
/**
|
|
* @throws ApiClientException
|
|
*/
|
|
public function getProfile(string $email, string $password): NotificationResponse|ProfileResponse
|
|
{
|
|
$data = [
|
|
'anfrage' => [
|
|
'user' => $this->config['bpn_username'],
|
|
'key' => $this->createKey($this->config['bpn_username'], $this->config['bpn_password'], static::TYPE_CUSTOMER_DATA),
|
|
'satz' => ['@typ' => static::TYPE_CUSTOMER_DATA],
|
|
'art' => 'Adressdaten',
|
|
'email' => $email,
|
|
'passwort' => $password,
|
|
],
|
|
];
|
|
|
|
return $this->sendRequest(static::TYPE_CUSTOMER_DATA, $data);
|
|
}
|
|
|
|
/**
|
|
* @throws ApiClientException
|
|
*/
|
|
public function updateProfile(User $user, string $password): NotificationResponse|ProfileResponse
|
|
{
|
|
if (null === $teamer = $user->getTeamer()) {
|
|
throw new ApiClientException('Invalid argument');
|
|
}
|
|
|
|
$data = [
|
|
'anfrage' => [
|
|
'user' => $this->config['bpn_username'],
|
|
'key' => $this->createKey($this->config['bpn_username'], $this->config['bpn_password'], static::TYPE_CUSTOMER_DATA),
|
|
'satz' => ['@typ' => static::TYPE_CUSTOMER_DATA],
|
|
'art' => 'Adressdaten_Ändern',
|
|
'email' => $user->getEmail(),
|
|
'passwort' => $password,
|
|
'idadresse' => $user->getBusProAddressId(),
|
|
'adressdaten' => $teamer->toPayload(),
|
|
],
|
|
];
|
|
|
|
return $this->sendRequest(static::TYPE_CUSTOMER_DATA, $data);
|
|
}
|
|
|
|
/**
|
|
* @throws ApiClientException
|
|
*/
|
|
public function resetPassword(string $email): NotificationResponse
|
|
{
|
|
$data = [
|
|
'anfrage' => [
|
|
'user' => $this->config['bpn_username'],
|
|
'key' => $this->createKey($this->config['bpn_username'], $this->config['bpn_password'], static::TYPE_CUSTOMER_DATA),
|
|
'satz' => ['@typ' => static::TYPE_CUSTOMER_DATA],
|
|
'art' => 'Passwort_Anfrage',
|
|
'email' => $email,
|
|
],
|
|
];
|
|
|
|
return $this->sendRequest(static::TYPE_CUSTOMER_DATA, $data);
|
|
}
|
|
|
|
/**
|
|
* @throws ApiClientException
|
|
*/
|
|
public function getCrmAttributes(string $email, string $password): NotificationResponse|CrmAttributesResponse
|
|
{
|
|
$data = [
|
|
'anfrage' => [
|
|
'user' => $this->config['bpn_username'],
|
|
'key' => $this->createKey($this->config['bpn_username'], $this->config['bpn_password'], static::TYPE_CUSTOMER_DATA),
|
|
'satz' => ['@typ' => static::TYPE_CUSTOMER_DATA],
|
|
'art' => 'SelektionCRM',
|
|
'email' => $email,
|
|
'passwort' => $password,
|
|
],
|
|
];
|
|
|
|
return $this->sendRequest(static::TYPE_CUSTOMER_DATA, $data);
|
|
}
|
|
|
|
/**
|
|
* @throws ApiClientException
|
|
*/
|
|
public function getBaseData(string $type): NotificationResponse|BaseDataResponse
|
|
{
|
|
$data = [
|
|
'anfrage' => [
|
|
'user' => $this->config['bpn_username'],
|
|
'key' => $this->createKey($this->config['bpn_username'], $this->config['bpn_password'], $type),
|
|
'satz' => ['@typ' => $type],
|
|
],
|
|
];
|
|
|
|
return $this->sendRequest($type, $data);
|
|
}
|
|
|
|
/**
|
|
* @throws ApiClientException
|
|
*/
|
|
private function sendRequest(string $type, array $data): mixed
|
|
{
|
|
$body = $this
|
|
->serializer
|
|
->serialize($data, 'xml')
|
|
;
|
|
|
|
try {
|
|
$response = $this->httpClient->request('GET', $this->config['bpn_url'], [
|
|
'query' => [
|
|
'operation' => $body,
|
|
]
|
|
]);
|
|
|
|
$xml = $response->getContent();
|
|
|
|
return $this->responseParser->parseXmlString($type, $xml);
|
|
} catch (\Throwable $e) {
|
|
}
|
|
|
|
$this->logger->error('API error', ['error' => $e->getMessage()]);
|
|
throw new ApiClientException($e->getMessage());
|
|
}
|
|
|
|
private function createKey(string $username, string $password, string $type): string
|
|
{
|
|
$date = (new \DateTimeImmutable())->format('Ymd');
|
|
|
|
return md5($username.$password.$date.$type);
|
|
}
|
|
|
|
private function resolveOptions(array $options): array
|
|
{
|
|
$optionsResolver = new OptionsResolver();
|
|
$optionsResolver->setRequired(['bpn_url', 'bpn_username', 'bpn_password']);
|
|
|
|
return $optionsResolver->resolve($options);
|
|
}
|
|
} |