Files
myep-team/src/BusProNet/ApiClient.php
T

203 lines
6.3 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\Component\Uid\Uuid;
class ApiClient
{
use ApiClientTrait;
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 SerializerInterface $serializer,
private readonly ResponseParser $responseParser,
private readonly LoggerInterface $logger,
array $options
) {
$this->config = $this->resolveOptions($options);
}
/**
* @throws ApiClientException
* @throws ResponseParserException
*/
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
* @throws ResponseParserException
*/
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
* @throws ResponseParserException
*/
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
* @throws ResponseParserException
*/
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
* @throws ResponseParserException
*/
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
* @throws ResponseParserException
*/
private function sendRequest(string $type, array $data): mixed
{
$requestId = (string) Uuid::v4();
$body = $this
->serializer
->serialize($data, 'xml')
;
if (true === $this->config['debug']) {
$this->logger->info('Request sent', [
'id' => $requestId,
'request' => $body,
]);
}
$socket = $this->connect(
$this->config['bpn_api_ip'],
$this->config['bpn_api_port'],
$this->config['max_retries']
);
$this->send($socket, $body);
$response = $this->receive($socket);
$this->disconnect($socket);
// message length (10 bytes) is prepended to actual message
$xml = substr($response, 10);
if (true === $this->config['debug']) {
$this->logger->info('Response received', [
'id' => $requestId,
'response' => $xml,
]);
}
return $this->responseParser->parseXmlString($type, $xml);
}
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_username',
'bpn_password',
'bpn_api_ip',
'bpn_api_port',
]);
$optionsResolver->setDefaults([
'max_retries' => 25,
'debug' => false,
]);
return $optionsResolver->resolve($options);
}
}