Skip to content

Commit

Permalink
make abstract tag service
Browse files Browse the repository at this point in the history
  • Loading branch information
temaotl committed Aug 9, 2024
1 parent e4704a4 commit d9f5f84
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 48 deletions.
3 changes: 2 additions & 1 deletion app/Facades/HfdTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
*/
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/EntityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 6 additions & 1 deletion app/Services/HfdTagService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
49 changes: 4 additions & 45 deletions app/Services/RsTagService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use App\Traits\ValidatorTrait;
use DOMXPath;

class RsTagService
class RsTagService extends TagService
{
use TagTrait,ValidatorTrait;

Expand Down Expand Up @@ -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);
}

Expand All @@ -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) {
Expand All @@ -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;
}
}
57 changes: 57 additions & 0 deletions app/Services/TagService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace App\Services;

use App\Models\Entity;
use DOMNode;
use DOMXPath;

abstract class TagService
{
abstract public function create(Entity $entity);

abstract public function delete(Entity $entity);

abstract public function update(Entity $entity);

protected function buildXPathQuery(string $value): string
{
return "//saml:AttributeValue[text()='$value']";
}

protected 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;
}

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;
}
}

0 comments on commit d9f5f84

Please sign in to comment.