Skip to content

Commit

Permalink
Centralisation of profile recognition
Browse files Browse the repository at this point in the history
  • Loading branch information
HorstOeko committed Jan 7, 2024
1 parent 25c91f2 commit 23dcbda
Show file tree
Hide file tree
Showing 9 changed files with 373 additions and 63 deletions.
3 changes: 3 additions & 0 deletions build/phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
<testsuite name="OrderPackageVersion">
<file>../tests/testcases/OrderPackageVersionTest.php</file>
</testsuite>
<testsuite name="OrderProfile">
<file>../tests/testcases/OrderProfileResolverTest.php</file>
</testsuite>
<testsuite name="OrderBase">
<file>../tests/testcases/OrderDocumentBaseTest.php</file>
</testsuite>
Expand Down
5 changes: 3 additions & 2 deletions src/OrderDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use GoetasWebservices\Xsd\XsdToPhpRuntime\Jms\Handler\XmlSchemaDateHandler;
use horstoeko\orderx\jms\OrderTypesHandler;
use horstoeko\orderx\OrderObjectHelper;
use horstoeko\orderx\OrderProfileResolver;
use horstoeko\stringmanagement\PathUtils;
use JMS\Serializer\Handler\HandlerRegistryInterface;
use JMS\Serializer\SerializerBuilder;
Expand Down Expand Up @@ -79,7 +80,7 @@ class OrderDocument
*
* @codeCoverageIgnore
*/
public function __construct(int $profile)
protected function __construct(int $profile)
{
$this->initProfile($profile);
$this->initObjectHelper();
Expand Down Expand Up @@ -132,7 +133,7 @@ public function getProfileDefinition(): array
private function initProfile(int $profile): OrderDocument
{
$this->profileId = $profile;
$this->profileDefinition = OrderProfiles::PROFILEDEF[$profile];
$this->profileDefinition = OrderProfileResolver::resolveProfileDefById($profile);

return $this;
}
Expand Down
19 changes: 3 additions & 16 deletions src/OrderDocumentBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,30 +66,17 @@ class OrderDocumentBuilder extends OrderDocument
*/
protected $currentPosition = null;

/**
* Constructor
*
* @codeCoverageIgnore
* @param int $profile
*/
public function __construct(int $profile)
{
parent::__construct($profile);

$this->initNewDocument();
}

/**
* Creates a new OrderDocumentBuilder with profile $profile
*
* @codeCoverageIgnore
*
* @param integer $profile
* @param integer $profileId
* @return OrderDocumentBuilder
*/
public static function createNew(int $profile): OrderDocumentBuilder
public static function createNew(int $profileId): OrderDocumentBuilder
{
return (new self($profile));
return (new static($profileId))->initNewDocument();
}

/**
Expand Down
22 changes: 2 additions & 20 deletions src/OrderDocumentPdfMerger.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@
namespace horstoeko\orderx;

use Exception;
use horstoeko\orderx\exception\OrderCannotFindProfileString;
use horstoeko\orderx\exception\OrderUnknownProfileException;
use horstoeko\orderx\OrderProfileResolver;
use horstoeko\orderx\OrderDocumentPdfBuilderAbstract;
use horstoeko\orderx\OrderProfiles;
use SimpleXMLElement;

/**
* Class representing the facillity adding existing XML data (file or data-string)
Expand Down Expand Up @@ -123,21 +120,6 @@ private function xmlDataIsFile(): bool
*/
private function getProfileDefinition(): array
{
$xmlContent = $this->getXmlContent();

$xmldocument = new SimpleXMLElement($xmlContent);
$typeelement = $xmldocument->xpath('/rsm:SCRDMCCBDACIOMessageStructure/rsm:ExchangedDocumentContext/ram:GuidelineSpecifiedDocumentContextParameter/ram:ID');

if (!is_array($typeelement) || !isset($typeelement[0])) {
throw new OrderCannotFindProfileString();
}

foreach (OrderProfiles::PROFILEDEF as $profile => $profiledef) {
if ($typeelement[0] == $profiledef["contextparameter"]) {
return $profiledef;
}
}

throw new OrderUnknownProfileException((string)$typeelement[0]);
return OrderProfileResolver::resolveProfileDef($this->getXmlContent());
}
}
23 changes: 5 additions & 18 deletions src/OrderDocumentReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@

use Closure;
use DateTime;
use SimpleXMLElement;
use OutOfRangeException;
use horstoeko\orderx\exception\OrderFileNotFoundException;
use horstoeko\orderx\OrderProfileResolver;
use horstoeko\stringmanagement\FileUtils;
use horstoeko\stringmanagement\PathUtils;
use horstoeko\stringmanagement\StringUtils;
use horstoeko\orderx\exception\OrderFileNotFoundException;
use horstoeko\orderx\exception\OrderCannotFindProfileString;
use horstoeko\orderx\exception\OrderUnknownProfileException;
use OutOfRangeException;

/**
* Class representing the document reader for incoming XML-Documents with
Expand Down Expand Up @@ -232,20 +230,9 @@ public static function readAndGuessFromFile(string $xmlfilename): OrderDocumentR
*/
public static function readAndGuessFromContent(string $xmlcontent): OrderDocumentReader
{
$xmldocument = new SimpleXMLElement($xmlcontent);
$typeelement = $xmldocument->xpath('/rsm:SCRDMCCBDACIOMessageStructure/rsm:ExchangedDocumentContext/ram:GuidelineSpecifiedDocumentContextParameter/ram:ID');

if (!is_array($typeelement) || !isset($typeelement[0])) {
throw new OrderCannotFindProfileString();
}

foreach (OrderProfiles::PROFILEDEF as $profile => $profiledef) {
if ($typeelement[0] == $profiledef["contextparameter"]) {
return (new self($profile))->readContent($xmlcontent);
}
}
$profileId = OrderProfileResolver::resolveProfileId($xmlcontent);

throw new OrderUnknownProfileException((string)$typeelement[0]);
return (new static($profileId))->readContent($xmlcontent);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/OrderObjectHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use DateTime;
use horstoeko\orderx\exception\OrderMimeTypeNotSupportedException;
use horstoeko\orderx\exception\OrderUnknownDateFormatException;
use horstoeko\orderx\OrderProfileResolver;
use horstoeko\stringmanagement\FileUtils;
use horstoeko\stringmanagement\StringUtils;
use MimeTyper\Repository\MimeDbRepository;
Expand Down Expand Up @@ -63,7 +64,7 @@ class OrderObjectHelper
public function __construct(int $profile)
{
$this->profile = $profile;
$this->profiledef = OrderProfiles::PROFILEDEF[$profile];
$this->profiledef = OrderProfileResolver::resolveProfileDefById($profile);
}

/**
Expand Down
111 changes: 111 additions & 0 deletions src/OrderProfileResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

/**
* This file is a part of horstoeko/orderx.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace horstoeko\orderx;

use Exception;
use horstoeko\orderx\exception\OrderCannotFindProfileString;
use horstoeko\orderx\exception\OrderUnknownProfileException;
use horstoeko\orderx\OrderProfiles;
use SimpleXMLElement;

/**
* Class representing the profile resolver
*
* @category Order-X
* @package Order-X
* @author D. Erling <[email protected]>
* @license https://opensource.org/licenses/MIT MIT
* @link https://github.com/horstoeko/orderx
*/
class OrderProfileResolver
{
/**
* Resolve profile id and profile definition by the content of $xmlContent
*
* @param string $xmlContent
* @return array
* @throws Exception
*/
public static function resolve(string $xmlContent): array
{
$xmldocument = new SimpleXMLElement($xmlContent);
$typeelement = $xmldocument->xpath('/rsm:SCRDMCCBDACIOMessageStructure/rsm:ExchangedDocumentContext/ram:GuidelineSpecifiedDocumentContextParameter/ram:ID');

if (!is_array($typeelement) || !isset($typeelement[0])) {
throw new OrderCannotFindProfileString();
}

/**
* @var int $profile
* @var array $profiledef
*/
foreach (OrderProfiles::PROFILEDEF as $profile => $profiledef) {
if ($typeelement[0] == $profiledef["contextparameter"]) {
return [$profile, $profiledef];
}
}

throw new OrderUnknownProfileException((string)$typeelement[0]);
}

/**
* Resolve profile id by the content of $xmlContent
*
* @param string $xmlContent
* @return int
* @throws Exception
*/
public static function resolveProfileId(string $xmlContent): int
{
return static::resolve($xmlContent)[0];
}

/**
* Resolve profile definition by the content of $xmlContent
*
* @param string $xmlContent
* @return array
* @throws Exception
*/
public static function resolveProfileDef(string $xmlContent): array
{
return static::resolve($xmlContent)[1];
}

/**
* Resolve profile id and profile definition by it's id
*
* @param integer $profileId
* @return array
* @throws Exception
*/
public static function resolveById(int $profileId): array
{
if (!isset(OrderProfiles::PROFILEDEF[$profileId])) {
throw new Exception('Could not determine the profile...');
}

return [$profileId, OrderProfiles::PROFILEDEF[$profileId]];
}

/**
* Resolve profile profile definition by it's id
*
* @param int $profileId
* @return array
* @throws Exception
*/
public static function resolveProfileDefById(int $profileId): array
{
$resolved = static::resolveById($profileId);

return $resolved[1];
}
}
12 changes: 6 additions & 6 deletions tests/testcases/OrderDocumentBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace horstoeko\orderx\tests\testcases;

use horstoeko\orderx\OrderDocument;
use horstoeko\orderx\OrderDocumentBuilder;
use horstoeko\orderx\OrderProfiles;
use horstoeko\orderx\tests\TestCase;

Expand All @@ -13,7 +14,7 @@ class OrderDocumentBaseTest extends TestCase
*/
public function testDocumentCreationBasic(): void
{
$doc = new OrderDocument(OrderProfiles::PROFILE_BASIC);
$doc = OrderDocumentBuilder::createNew(OrderProfiles::PROFILE_BASIC);
$this->assertNotNull($doc);
$this->assertEquals(OrderProfiles::PROFILE_BASIC, $doc->getProfileId());
$this->assertArrayHasKey("contextparameter", $doc->getProfileDefinition());
Expand All @@ -27,7 +28,7 @@ public function testDocumentCreationBasic(): void
*/
public function testDocumentCreationComfort(): void
{
$doc = new OrderDocument(OrderProfiles::PROFILE_COMFORT);
$doc = OrderDocumentBuilder::createNew(OrderProfiles::PROFILE_COMFORT);
$this->assertNotNull($doc);
$this->assertEquals(OrderProfiles::PROFILE_COMFORT, $doc->getProfileId());
$this->assertArrayHasKey("contextparameter", $doc->getProfileDefinition());
Expand All @@ -41,7 +42,7 @@ public function testDocumentCreationComfort(): void
*/
public function testDocumentCreationExtended(): void
{
$doc = new OrderDocument(OrderProfiles::PROFILE_EXTENDED);
$doc = OrderDocumentBuilder::createNew(OrderProfiles::PROFILE_EXTENDED);
$this->assertNotNull($doc);
$this->assertEquals(OrderProfiles::PROFILE_EXTENDED, $doc->getProfileId());
$this->assertArrayHasKey("contextparameter", $doc->getProfileDefinition());
Expand All @@ -55,15 +56,14 @@ public function testDocumentCreationExtended(): void
*/
public function testDocumentInternals(): void
{
$doc = new OrderDocument(OrderProfiles::PROFILE_EXTENDED);
$doc = OrderDocumentBuilder::createNew(OrderProfiles::PROFILE_EXTENDED);
$property = $this->getPrivatePropertyFromClassname('horstoeko\orderx\OrderDocument', 'serializerBuilder');
$this->assertNotNull($property->getValue($doc));
$property = $this->getPrivatePropertyFromClassname('horstoeko\orderx\OrderDocument', 'serializer');
$this->assertNotNull($property->getValue($doc));
$property = $this->getPrivatePropertyFromClassname('horstoeko\orderx\OrderDocument', 'orderObject');
$this->assertNull($property->getValue($doc));
$this->assertNotNull($property->getValue($doc));
$property = $this->getPrivatePropertyFromClassname('horstoeko\orderx\OrderDocument', 'objectHelper');
$this->assertNotNull($property->getValue($doc));
$this->assertNull($doc->getOrderObject());
}
}
Loading

0 comments on commit 23dcbda

Please sign in to comment.