From 69b6a0f395b914d4f03f1e003209695d1b19ec16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Fromme?= Date: Tue, 12 Sep 2023 15:08:23 +0200 Subject: [PATCH] Feat: Make CRM ids configurable --- .env | 4 ++++ config/services.yaml | 17 +++++++++++------ src/BusProNet/ApiClient.php | 5 +++-- src/BusProNet/Model/CrmAttribute.php | 4 ---- src/BusProNet/ResponseParser.php | 27 +++++++++++++++++++++++---- 5 files changed, 41 insertions(+), 16 deletions(-) diff --git a/.env b/.env index 6ff6389..13bb19d 100644 --- a/.env +++ b/.env @@ -43,3 +43,7 @@ MESSENGER_TRANSPORT_DSN=doctrine://default?auto_setup=0 APP_BPN_USER= APP_BPN_PASSWORD= APP_BPN_ENDPOINT= + +APP_BPN_CRM_ID_ADMIN=1292 +APP_BPN_CRM_ID_MANAGER=1293 +APP_BPN_CRM_ID_TEAMER=1070 diff --git a/config/services.yaml b/config/services.yaml index bf03a27..c13812c 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -3,14 +3,15 @@ parameters: bpn_username: '%env(APP_BPN_USER)%' bpn_password: '%env(APP_BPN_PASSWORD)%' + bpn_crm_id_admin: '%env(int:APP_BPN_CRM_ID_ADMIN)%' + bpn_crm_id_manager: '%env(int:APP_BPN_CRM_ID_MANAGER)%' + bpn_crm_id_teamer: '%env(int:APP_BPN_CRM_ID_TEAMER)%' + services: - # default configuration for services in *this* file _defaults: - autowire: true # Automatically injects dependencies in your services. - autoconfigure: true # Automatically registers your services as commands, event subscribers, etc. + autowire: true + autoconfigure: true - # makes classes in src/ available to be used as services - # this creates a service per class whose id is the fully-qualified class name App\: resource: '../src/' exclude: @@ -20,4 +21,8 @@ services: App\BusProNet\ApiClient: arguments: - $options: { 'bpn_username': '%bpn_username%', 'bpn_password': '%bpn_password%', 'bpn_url': '%bpn_url%' } \ No newline at end of file + $options: { 'bpn_username': '%bpn_username%', 'bpn_password': '%bpn_password%', 'bpn_url': '%bpn_url%' } + + App\BusProNet\ResponseParser: + arguments: + $options: { 'bpn_crm_id_admin': '%bpn_crm_id_admin%', 'bpn_crm_id_manager': '%bpn_crm_id_manager%', 'bpn_crm_id_teamer': '%bpn_crm_id_teamer%' } \ No newline at end of file diff --git a/src/BusProNet/ApiClient.php b/src/BusProNet/ApiClient.php index 4ec9a8b..c420dbf 100644 --- a/src/BusProNet/ApiClient.php +++ b/src/BusProNet/ApiClient.php @@ -14,6 +14,7 @@ class ApiClient public function __construct( private readonly HttpClientInterface $httpClient, private readonly SerializerInterface $serializer, + private readonly ResponseParser $responseParser, array $options ) { $this->config = $this->resolveOptions($options); @@ -49,7 +50,7 @@ class ApiClient $xml = $response->getContent(); - return (new ResponseParser())->parseXmlString($xml); + return $this->responseParser->parseXmlString($xml); } catch (\Throwable $e) { } @@ -89,7 +90,7 @@ class ApiClient $xml = $response->getContent(); - return (new ResponseParser())->parseXmlString($xml); + return $this->responseParser->parseXmlString($xml); } catch (\Throwable $e) { } diff --git a/src/BusProNet/Model/CrmAttribute.php b/src/BusProNet/Model/CrmAttribute.php index 7f05e7f..22b6900 100644 --- a/src/BusProNet/Model/CrmAttribute.php +++ b/src/BusProNet/Model/CrmAttribute.php @@ -4,10 +4,6 @@ namespace App\BusProNet\Model; class CrmAttribute { - public const ATTR_ID_ADMIN = 1071; - public const ATTR_ID_MANAGER = 1072; - public const ATTR_ID_TEAMER = 1070; - private ?int $id = null; private ?string $label = null; private bool $selected = false; diff --git a/src/BusProNet/ResponseParser.php b/src/BusProNet/ResponseParser.php index 8ac0fc1..631b66b 100644 --- a/src/BusProNet/ResponseParser.php +++ b/src/BusProNet/ResponseParser.php @@ -9,9 +9,17 @@ use App\BusProNet\Model\CrmAttributesResponse; use App\BusProNet\Model\CrmAttributeGroup; use App\BusProNet\Model\ProfileResponse; use App\BusProNet\Model\BaseResponse; +use Symfony\Component\OptionsResolver\OptionsResolver; class ResponseParser { + private array $config; + + public function __construct(array $options) + { + $this->config = $this->resolveOptions($options); + } + /** * @throws ResponseParserException */ @@ -55,7 +63,7 @@ class ResponseParser $title = (string) $xml->xpath('adressdaten/titel')[0]; $gender = (string) $xml->xpath('adressdaten/geschlecht')[0]; - $gender = $gender === 'W' ? 'f' : strtolower($gender); + $gender = strtolower($gender) === 'w' ? 'f' : strtolower($gender); $date = $xml->xpath('adressdaten/geburtsdatum'); $dateOfBirth = $date ?\DateTimeImmutable::createFromFormat('d.m.Y', (string) $date[0]) : null; @@ -122,13 +130,13 @@ class ResponseParser ; $attributes[] = $attribute; - if (CrmAttribute::ATTR_ID_ADMIN === $attribute->getId() && true === $attribute->isSelected()) { + if ($this->config['bpn_crm_id_admin'] === $attribute->getId() && true === $attribute->isSelected()) { $isAdmin = true; } - if (CrmAttribute::ATTR_ID_MANAGER === $attribute->getId() && true === $attribute->isSelected()) { + if ($this->config['bpn_crm_id_manager'] === $attribute->getId() && true === $attribute->isSelected()) { $isManager = true; } - if (CrmAttribute::ATTR_ID_TEAMER === $attribute->getId() && true === $attribute->isSelected()) { + if ($this->config['bpn_crm_id_teamer'] === $attribute->getId() && true === $attribute->isSelected()) { $isTeamer = true; } } @@ -146,4 +154,15 @@ class ResponseParser return $response; } + + private function resolveOptions(array $options): array + { + $optionsResolver = new OptionsResolver(); + $optionsResolver->setRequired(['bpn_crm_id_admin', 'bpn_crm_id_manager', 'bpn_crm_id_teamer']); + $optionsResolver->setAllowedTypes('bpn_crm_id_admin', 'int'); + $optionsResolver->setAllowedTypes('bpn_crm_id_manager', 'int'); + $optionsResolver->setAllowedTypes('bpn_crm_id_teamer', 'int'); + + return $optionsResolver->resolve($options); + } } \ No newline at end of file