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');
+20
View File
@@ -0,0 +1,20 @@
<?php
namespace App\BusProNet\Model;
class CountriesResponse extends BaseResponse
{
private array $countries = [];
public function getCountries(): array
{
return $this->countries;
}
public function setCountries(array $countries): static
{
$this->countries = $countries;
return $this;
}
}
+59
View File
@@ -0,0 +1,59 @@
<?php
namespace App\BusProNet\Model;
class Country
{
private ?int $id = null;
private ?string $name = null;
private ?string $token = null;
private ?string $nationality = null;
public function getId(): ?int
{
return $this->id;
}
public function setId(?int $id): static
{
$this->id = $id;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(?string $name): static
{
$this->name = $name;
return $this;
}
public function getToken(): ?string
{
return $this->token;
}
public function setToken(?string $token): static
{
$this->token = $token;
return $this;
}
public function getNationality(): ?string
{
return $this->nationality;
}
public function setNationality(?string $nationality): static
{
$this->nationality = $nationality;
return $this;
}
}
+26 -1
View File
@@ -4,6 +4,8 @@ namespace App\BusProNet;
use App\BusProNet\Model\Address;
use App\BusProNet\Model\Communication;
use App\BusProNet\Model\CountriesResponse;
use App\BusProNet\Model\Country;
use App\BusProNet\Model\CrmAttribute;
use App\BusProNet\Model\CrmAttributesResponse;
use App\BusProNet\Model\CrmAttributeGroup;
@@ -39,6 +41,8 @@ class ResponseParser
case 'SelektionCRM':
return $this->createCrmAttributesResponse($xml);
}
case 'STAMMLAENDER':
return $this->createCountriesResponse($xml);
}
throw new ResponseParserException('Unable to parse XML response');
@@ -63,7 +67,7 @@ class ResponseParser
$title = (string) $xml->xpath('adressdaten/titel')[0];
$gender = (string) $xml->xpath('adressdaten/geschlecht')[0];
$gender = strtolower($gender) === 'w' ? 'f' : strtolower($gender);
$gender = strtoupper($gender) === 'W' ? 'F' : strtoupper($gender);
$date = $xml->xpath('adressdaten/geburtsdatum');
$dateOfBirth = $date ?\DateTimeImmutable::createFromFormat('d.m.Y', (string) $date[0]) : null;
@@ -155,6 +159,27 @@ class ResponseParser
return $response;
}
public function createCountriesResponse(\SimpleXMLElement $xml): CountriesResponse
{
$countries = [];
foreach ($xml->xpath('laender/land') as $item) {
$country = new Country();
$country
->setId((int)$item->attributes()['id'])
->setName((string)$item->attributes()['bezeichnung'])
->setToken((string)$item->attributes()['kuerzel'])
->setNationality((string)$item->attributes()['nationalitaet'])
;
$countries[] = $country;
}
$response = new CountriesResponse();
$response->setCountries($countries);
return $response;
}
private function resolveOptions(array $options): array
{
$optionsResolver = new OptionsResolver();