From d9f5f84a8ce481ad8e4da52c82d999defaf75ad2 Mon Sep 17 00:00:00 2001 From: Artem Otliaguzov Date: Fri, 9 Aug 2024 14:26:28 +0200 Subject: [PATCH] make abstract tag service --- app/Facades/HfdTag.php | 3 +- app/Http/Controllers/EntityController.php | 2 +- app/Services/HfdTagService.php | 7 ++- app/Services/RsTagService.php | 49 ++----------------- app/Services/TagService.php | 57 +++++++++++++++++++++++ 5 files changed, 70 insertions(+), 48 deletions(-) create mode 100644 app/Services/TagService.php diff --git a/app/Facades/HfdTag.php b/app/Facades/HfdTag.php index 4d12891..7d74665 100644 --- a/app/Facades/HfdTag.php +++ b/app/Facades/HfdTag.php @@ -10,8 +10,9 @@ /** * Class HfdTag facade * - * @method static string create(string $xml_document) + * @method static false|string create(Entity $entity) * @method static void delete(Entity $entity) + * @method static false|string createFromXml(string $xml_document) * @method static void deleteByXpath( DOMXPath $xPath) * @method static false|string update(Entity $entity) */ diff --git a/app/Http/Controllers/EntityController.php b/app/Http/Controllers/EntityController.php index b4721d3..3febae1 100644 --- a/app/Http/Controllers/EntityController.php +++ b/app/Http/Controllers/EntityController.php @@ -101,7 +101,7 @@ public function store(StoreEntity $request) if ($new_entity['type'] === 'idp') { $new_entity = array_merge($new_entity, ['hfd' => true]); - $new_entity['xml_file'] = HfdTag::create($new_entity['xml_file']); + $new_entity['xml_file'] = HfdTag::createFromXml($new_entity['xml_file']); } $entity = Entity::create($new_entity); diff --git a/app/Services/HfdTagService.php b/app/Services/HfdTagService.php index 7f77615..9fe3cf5 100644 --- a/app/Services/HfdTagService.php +++ b/app/Services/HfdTagService.php @@ -13,7 +13,12 @@ class HfdTagService private string $value = 'http://refeds.org/category/hide-from-discovery'; - public function create(string $xml_document): false|string + public function create(Entity $entity): false|string + { + return $this->createFromXml($entity->xml_file); + } + + public function createFromXml(string $xml_document): false|string { $mdURI = config('xmlNameSpace.md'); $mdattrURI = config('xmlNameSpace.mdattr'); diff --git a/app/Services/RsTagService.php b/app/Services/RsTagService.php index e2c8ecf..ce24118 100644 --- a/app/Services/RsTagService.php +++ b/app/Services/RsTagService.php @@ -7,7 +7,7 @@ use App\Traits\ValidatorTrait; use DOMXPath; -class RsTagService +class RsTagService extends TagService { use TagTrait,ValidatorTrait; @@ -51,7 +51,7 @@ public function delete(Entity $entity): false|string public function deleteByXpath(DOMXPath $xPath): void { - $xpathQuery = $this->buildXPathQuery(); + $xpathQuery = $this->buildXPathQuery($this->value); $this->DeleteAllTags($xpathQuery, $xPath); } @@ -74,28 +74,13 @@ public function update(Entity $entity): false|string private function hasResearchAndScholarshipTag(string $xml_document): bool { - $xpathQuery = $this->buildXPathQuery(); + $xpathQuery = $this->buildXPathQuery($this->value); return $this->hasXpathQueryInDocument($xml_document, $xpathQuery); } - private function buildXPathQuery(): string - { - return "//saml:AttributeValue[text()='$this->value']"; - } - - private function getRootTag(\DOMXPath $xPath): \DOMNode - { - $rootTag = $xPath->query("//*[local-name()='EntityDescriptor']")->item(0); - if (! $rootTag) { - throw new \RuntimeException('Root tag EntityDescriptor not found'); - } - - return $rootTag; - } - - private function getOrCreateAttribute(\DOMXPath $xPath, \DOMDocument $dom, \DOMNode $entityAttributes, string $samlURI, bool $isIdp): \DOMNode + protected function getOrCreateAttribute(\DOMXPath $xPath, \DOMDocument $dom, \DOMNode $entityAttributes, string $samlURI, bool $isIdp): \DOMNode { $attribute = $xPath->query('//mdattr:EntityAttributes/saml:Attribute', $entityAttributes); if ($attribute->length === 0) { @@ -109,30 +94,4 @@ private function getOrCreateAttribute(\DOMXPath $xPath, \DOMDocument $dom, \DOMN return $attribute; } - - private function getOrCreateEntityAttributes(\DOMXPath $xPath, \DOMDocument $dom, \DOMNode $extensions, string $mdattrURI): \DOMNode - { - $entityAttributes = $xPath->query('//mdattr:EntityAttributes'); - if ($entityAttributes->length === 0) { - $entityAttributes = $dom->createElementNS($mdattrURI, 'mdattr:EntityAttributes'); - $extensions->appendChild($entityAttributes); - } else { - $entityAttributes = $entityAttributes->item(0); - } - - return $entityAttributes; - } - - private function getOrCreateExtensions(\DOMXPath $xPath, \DOMDocument $dom, \DOMNode $rootTag, string $mdURI): \DOMNode - { - $extensions = $xPath->query('//md:Extensions'); - if ($extensions->length === 0) { - $extensions = $dom->createElementNS($mdURI, 'md:Extensions'); - $rootTag->appendChild($extensions); - } else { - $extensions = $extensions->item(0); - } - - return $extensions; - } } diff --git a/app/Services/TagService.php b/app/Services/TagService.php new file mode 100644 index 0000000..2ffb117 --- /dev/null +++ b/app/Services/TagService.php @@ -0,0 +1,57 @@ +query("//*[local-name()='EntityDescriptor']")->item(0); + if (! $rootTag) { + throw new \RuntimeException('Root tag EntityDescriptor not found'); + } + + return $rootTag; + } + + protected function getOrCreateExtensions(DOMXPath $xPath, \DOMDocument $dom, \DOMNode $rootTag, string $mdURI): \DOMNode + { + $extensions = $xPath->query('//md:Extensions'); + if ($extensions->length === 0) { + $extensions = $dom->createElementNS($mdURI, 'md:Extensions'); + $rootTag->appendChild($extensions); + } else { + $extensions = $extensions->item(0); + } + + return $extensions; + } + + protected function getOrCreateEntityAttributes(\DOMXPath $xPath, \DOMDocument $dom, \DOMNode $extensions, string $mdattrURI): \DOMNode + { + $entityAttributes = $xPath->query('//mdattr:EntityAttributes'); + if ($entityAttributes->length === 0) { + $entityAttributes = $dom->createElementNS($mdattrURI, 'mdattr:EntityAttributes'); + $extensions->appendChild($entityAttributes); + } else { + $entityAttributes = $entityAttributes->item(0); + } + + return $entityAttributes; + } +}