This commit is contained in:
Björn Fromme
2023-07-07 18:19:15 +02:00
parent 1e9d1c5ac4
commit 305699b1ac
28 changed files with 1428 additions and 63 deletions
+131
View File
@@ -0,0 +1,131 @@
<?php
namespace App\BusProNet;
use App\BusProNet\Model\CrmAttributeSelection;
use App\BusProNet\Model\Profile;
use App\BusProNet\Model\ErrorResponse;
use App\BusProNet\Model\Result;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class ApiClient
{
private array $config;
public function __construct(
private readonly HttpClientInterface $httpClient,
private readonly SerializerInterface $serializer,
array $options
) {
$this->config = $this->resolveOptions($options);
}
/**
* @throws ApiClientException
*/
public function getProfile(string $email, string $password): mixed
{
$data = [
'anfrage' => [
'user' => $this->config['bpn_username'],
'key' => $this->createKey($this->config['bpn_username'], $this->config['bpn_password'], 'KUNDENKONTO'),
'satz' => ['@typ' => 'KUNDENKONTO'],
'art' => 'Adressdaten',
'email' => $email,
'passwort' => md5($password),
],
];
$body = $this
->serializer
->serialize($data, 'xml')
;
try {
$response = $this->httpClient->request('GET', $this->config['bpn_url'], [
'query' => [
'operation' => $body,
]
]);
$xml = $response->getContent();
if (str_contains($xml, 'HINWEIS')) {
return $this->serializer->deserialize($xml, ErrorResponse::class, 'xml');
}
return $this->serializer->deserialize($xml, Profile::class, 'xml');
} catch (\Throwable $e) {
}
throw new ApiClientException($e->getMessage());
}
public function updateProfile(): void
{}
/**
* @throws ApiClientException
*/
public function getCrmSelection(string $email, string $password): mixed
{
$data = [
'anfrage' => [
'user' => $this->config['bpn_username'],
'key' => $this->createKey($this->config['bpn_username'], $this->config['bpn_password'], 'KUNDENKONTO'),
'satz' => ['@typ' => 'KUNDENKONTO'],
'art' => 'SelektionCRM',
'email' => $email,
'passwort' => md5($password),
],
];
$body = $this
->serializer
->serialize($data, 'xml')
;
try {
$response = $this->httpClient->request('GET', $this->config['bpn_url'], [
'query' => [
'operation' => $body,
]
]);
$xml = $response->getContent();
if (str_contains($xml, 'HINWEIS')) {
return $this->serializer->deserialize($xml, ErrorResponse::class, 'xml');
}
return $this->serializer->deserialize($xml, CrmAttributeSelection::class, 'xml');
} catch (\Throwable $e) {
}
throw new ApiClientException($e->getMessage());
}
/**
* @Creates key for BusPro API access according to documentation
*/
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);
}
}