WIP: Implement profile editing

This commit is contained in:
Björn Fromme
2023-09-17 19:36:18 +02:00
parent 97c5a686af
commit bfc5e5e242
17 changed files with 498 additions and 18 deletions
+79 -8
View File
@@ -3,8 +3,11 @@
namespace App\BusProNet;
use App\BusProNet\Model\BaseResponse;
use App\Entity\User;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class ApiClient
@@ -15,6 +18,7 @@ class ApiClient
private readonly HttpClientInterface $httpClient,
private readonly SerializerInterface $serializer,
private readonly ResponseParser $responseParser,
private readonly CacheInterface $cache,
array $options
) {
$this->config = $this->resolveOptions($options);
@@ -38,8 +42,7 @@ class ApiClient
$body = $this
->serializer
->serialize($data, 'xml')
;
->serialize($data, 'xml');
try {
$response = $this->httpClient->request('GET', $this->config['bpn_url'], [
@@ -57,8 +60,44 @@ class ApiClient
throw new ApiClientException($e->getMessage());
}
public function updateProfile(): void
{}
public function updateProfile(User $user, string $password): BaseResponse
{
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'], 'KUNDENKONTO'),
'satz' => ['@typ' => 'KUNDENKONTO'],
'art' => 'Adressdaten_Ändern',
'email' => $user->getEmail(),
'passwort' => md5($password),
'idadresse' => $user->getBusProAddressId(),
'adressdaten' => $teamer->toPayload(),
],
];
$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($xml);
} catch (\Throwable $e) {
}
throw new ApiClientException($e->getMessage());
}
public function resetPassword(string $email): BaseResponse
{
@@ -74,8 +113,7 @@ class ApiClient
$body = $this
->serializer
->serialize($data, 'xml')
;
->serialize($data, 'xml');
try {
$response = $this->httpClient->request('GET', $this->config['bpn_url'], [
@@ -111,8 +149,7 @@ class ApiClient
$body = $this
->serializer
->serialize($data, 'xml')
;
->serialize($data, 'xml');
try {
$response = $this->httpClient->request('GET', $this->config['bpn_url'], [
@@ -130,6 +167,40 @@ class ApiClient
throw new ApiClientException($e->getMessage());
}
public function getCountries(): BaseResponse
{
return $this->cache->get('bpn_countries', function (ItemInterface $item) {
$item->expiresAfter(3600);
$data = [
'anfrage' => [
'user' => $this->config['bpn_username'],
'key' => $this->createKey($this->config['bpn_username'], $this->config['bpn_password'], 'STAMMLAENDER'),
'satz' => ['@typ' => 'STAMMLAENDER'],
],
];
$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($xml);
} catch (\Throwable $e) {
}
throw new ApiClientException($e->getMessage());
});
}
private function createKey(string $username, string $password, string $type): string
{
$date = (new \DateTimeImmutable())->format('Ymd');