feat: refactoring of xml parsers

This commit is contained in:
Björn Fromme
2025-01-24 17:10:49 +01:00
parent 11f1508640
commit 6730c243f6
49 changed files with 1414 additions and 1235 deletions
@@ -0,0 +1,95 @@
<?php
namespace App\BusProNet\XmlParser;
use App\BusProNet\Model\CrmAction;
use App\BusProNet\Model\CrmAttributes;
use App\BusProNet\Model\CrmSelection;
use App\BusProNet\Model\CrmSelectionGroup;
use App\BusProNet\TypeConversionTrait;
use Symfony\Component\DomCrawler\Crawler;
class CrmAttributesResponseParser
{
use TypeConversionTrait;
private const BPN_CRM_ID_ADMIN = 1292;
private const BPN_CRM_ID_MANAGER = 1293;
private const BPN_CRM_ID_TEAMER = 1070;
public function parse(Crawler $result): CrmAttributes
{
$groups = [];
$actions = [];
$roles = [];
$hotelCode = null;
$result
->filterXPath('//selektionsmerkmale/selektionsgruppe')
->each(function (Crawler $node) use (&$groups, &$roles, &$hotelCode) {
$group = new CrmSelectionGroup();
$group->id = (int) $node->attr('id');
$group->label = $node->attr('bezeichnung');
$attributes = [];
$node
->filterXPath('//selektion')
->each(function (Crawler $node) use (&$attributes, &$roles, &$hotelCode) {
$attribute = new CrmSelection();
$attribute->id = (int) $node->attr('id');
$attribute->label = $node->attr('bezeichnung');
$attribute->mutable = $this->stringToBool($node->attr('aenderbar'));
$attribute->selected = $this->stringToBool($node->attr('auswahl'));
if (1 === preg_match('/^Hausleitung ([A-Z0-9]+)$/', $attribute->label, $matches) && true === $attribute->selected) {
$roles[] = 'ROLE_HOUSE_MANAGER';
$hotelCode = $matches[1];
}
if (static::BPN_CRM_ID_ADMIN === $attribute->id && true === $attribute->selected) {
$roles[] = 'ROLE_ADMIN';
}
if (static::BPN_CRM_ID_MANAGER === $attribute->id && true === $attribute->selected) {
$roles[] = 'ROLE_MANAGER';
}
if (static::BPN_CRM_ID_TEAMER === $attribute->id && true === $attribute->selected) {
$roles[] = 'ROLE_TEAMER';
}
$attributes[] = $attribute;
})
;
$group->selections = $attributes;
$groups[] = $group;
})
;
$result
->filterXPath('//crmaktionen/crmaktion')
->each(function (Crawler $node) use (&$actions) {
$crmAction = new CrmAction();
$crmAction->id = (int) $node->attr('id');
$crmAction->code = $node->attr('code');
$crmAction->label = $node->attr('bezeichnung');
$crmAction->mutable = $this->stringToBool($node->attr('aenderbar'));
$crmAction->selected = $this->stringToBool($node->attr('auswahl'));
$actions[] = $crmAction;
})
;
$response = new CrmAttributes();
$response->selectionGroups = $groups;
$response->crmActions = $actions;
$response->roles = $roles;
$response->admin = in_array('ROLE_ADMIN', $roles);
$response->manager = in_array('ROLE_MANAGER', $roles);
$response->houseManager = in_array('ROLE_HOUSE_MANAGER', $roles);
$response->teamer = in_array('ROLE_TEAMER', $roles);
$response->hotelCode = $hotelCode;
return $response;
}
}