Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
chlulei committed Oct 25, 2023
1 parent b7fe52e commit c976920
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@
use ImportHandler\I\File\XML\Node\Info\ilTreeInterface as ilXMLFileNodeInfoTreeInterface;
use ImportHandler\I\File\XSD\ilHandlerInterface as ilXSDFileHandlerInterface;
use ImportStatus\Exception\ilException as ilImportStatusException;
use ImportStatus\I\ilCollectionInterface as ilImportStatusCollectionInterface;
use ImportStatus\I\ilFactoryInterface as ilImportStatusFactoryInterface;
use ImportHandler\I\Parser\ilFactoryInterface as ilParserFactoryInterface;
use ImportHandler\I\File\XSD\ilFactoryInterface as ilXSDFileFactoryInterface;
use ImportStatus\StatusType;
use ImportHandler\I\File\Path\ilFactoryInterface as ilFilePathFactoryInterface;
use ImportHandler\I\File\Path\ilHandlerInterface as ilFilePathHandlerInterface;
use ImportHandler\I\File\XML\Node\Info\Attribute\ilFactoryInterface as ilXMlFileInfoNodeAttributeFactoryInterface;
use ImportHandler\I\File\XML\Node\Info\ilHandlerInterface as ilXMLFileNodeInfoInterface;
use Schema\ilXmlSchemaFactory;
use ILIAS\Data\Version;
use SplFileInfo;

class ilHandler extends ilXMLFileHandler implements ilXMLExportFileHandlerInterface
Expand All @@ -45,6 +48,10 @@ class ilHandler extends ilXMLFileHandler implements ilXMLExportFileHandlerInterf
protected ilXMlFileInfoNodeAttributeFactoryInterface $attribute;
protected ilLogger $logger;

protected Version $version;
protected string $type;
protected string $subtype;

public function __construct(
ilImportStatusFactoryInterface $status,
ilXmlSchemaFactory $schema,
Expand All @@ -70,36 +77,55 @@ public function withFileInfo(SplFileInfo $file_info): ilHandler
return $clone;
}

/**
* @throws ilImportStatusException
*/
public function getXSDFileHandler(): ilXSDFileHandlerInterface
public function loadExportInfo(): ilImportStatusCollectionInterface
{
$path_to_export = $this->path->handler()
->withStartAtRoot(true)
->withNode($this->path->node()->simple()->withName('exp:Export'));
$export_node_info = $this->parser->handler()
->withFileHandler($this)
->getNodeInfoAt($path_to_export)
->current();
$type_str = $export_node_info->getValueOfAttribute('Entity');
$types = (str_contains($type_str, '_'))
? explode('_', $type_str)
: [$type_str];
$latest_file_info = count($types) === 1
? $this->schema->getLatest($types[0])
: $this->schema->getLatest($types[0], $types[1]);
if (is_null($latest_file_info)) {
$statuses = $this->status->collection()->withAddedStatus($this->status->handler()
->withType(StatusType::DEBUG)
->withContent($this->status->content()->builder()->string()->withString(
'Missing schema xsd file for entity of type: ' . implode('_', $types)
)));
$exception = $this->status->exception($statuses->toString(StatusType::DEBUG));
$exception->setStatuses($statuses);
throw $exception;
$node_info = null;
try {
$node_info = $this->parser->handler()
->withFileHandler($this)
->getNodeInfoAt($path_to_export)
->current();
} catch (ilImportStatusException $e) {
return $e->getStatuses();
}
return $this->xsd_file->handler()->withFileInfo($latest_file_info);
$type_str = $node_info->getValueOfAttribute('Entity');
$types = str_contains($type_str, '_')
? explode('_', $type_str)
: [$type_str, ''];
$version_str = $node_info->getValueOfAttribute('SchemaVersion');
$this->type = $types[0];
$this->subtype = $types[1];
$this->version = new Version($version_str);
return $this->status->collection();
}

public function getVersion(): Version
{
return $this->version;
}

public function getType(): string
{
return $this->type;
}

public function getSubType(): string
{
return $this->subtype;
}

/**
* @throws ilImportStatusException
*/
public function getXSDFileHandler(): ilXSDFileHandlerInterface|null
{
$latest_file_info = $this->schema->getByVersionOrLatest($this->version, $this->type, $this->subtype);
return is_null($latest_file_info)
? null
: $this->xsd_file->handler()->withFileInfo($latest_file_info);
}

public function getILIASPath(ilXMLFileNodeInfoTreeInterface $component_tree): string
Expand All @@ -117,4 +143,9 @@ public function getILIASPath(ilXMLFileNodeInfoTreeInterface $component_tree): st
? ''
: $node->getAttributePath('Title', DIRECTORY_SEPARATOR);
}

public function isContainerExportXML(): bool
{
return $this->getSubPathToDirBeginningAtPathEnd('temp')->pathContainsFolderName('Container');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,7 @@ public function getExportObjectType(): ilExportObjectType
$export_file_node_info->count() > 0 &&
$export_set_node_info->count() > 0
) {
$statuses = $this->status->collection()->withAddedStatus(
$this->status->handler()->withType(StatusType::FAILED)->withContent(
$this->status->content()->builder()->string()->withString(
"XML:"
. $this->getSubPathToDirBeginningAtPathEnd('temp')->getFilePath()
. "\nFound export and set elements in manifest xml."
)
)
);
$exception = $this->status->exception($statuses->toString(StatusType::FAILED));
$exception->setStatuses($statuses);
throw $exception;
return ilExportObjectType::MIXED;
}
if ($export_file_node_info->count() > 0) {
return ilExportObjectType::EXPORT_FILE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

enum ilExportObjectType
{
case MIXED;
case EXPORT_SET;
case EXPORT_FILE;
case NONE;
Expand Down
15 changes: 0 additions & 15 deletions Services/Export/classes/ImportHandler/File/class.ilHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,20 @@ public function withFileInfo(SplFileInfo $file_info): ilFileHandlerInterface
return $clone;
}

protected function checkIfFileInfoIsSet(): void
{
if(!isset($this->xml_file_info)) {
throw new ilImportException("Missing file info.");
}
}

public function getFileName(): string
{
$this->checkIfFileInfoIsSet();
return $this->xml_file_info->getFilename();
}

public function getFilePath(): string
{
$this->checkIfFileInfoIsSet();
return $this->fileExists()
? $this->xml_file_info->getRealPath()
: $this->xml_file_info->getPath() . DIRECTORY_SEPARATOR . $this->xml_file_info->getFilename();
}

public function getSubPathToDirBeginningAtPathEnd(string $dir_name): ilFileHandlerInterface
{
$this->checkIfFileInfoIsSet();
$parts = explode(DIRECTORY_SEPARATOR, $this->getFilePath());
$trimmed_str = '';
for ($i = count($parts) - 1; $i >= 0; $i--) {
Expand All @@ -76,7 +66,6 @@ public function getSubPathToDirBeginningAtPathEnd(string $dir_name): ilFileHandl

public function getSubPathToDirBeginningAtPathStart(string $dir_name): ilFileHandlerInterface
{
$this->checkIfFileInfoIsSet();
$parts = explode(DIRECTORY_SEPARATOR, $this->getFilePath());
$trimmed_str = '';
for ($i = 0; $i < count($parts); $i++) {
Expand All @@ -94,19 +83,16 @@ public function getSubPathToDirBeginningAtPathStart(string $dir_name): ilFileHan

public function getPathToFileLocation(): string
{
$this->checkIfFileInfoIsSet();
return $this->xml_file_info->getPath();
}

public function fileExists(): bool
{
$this->checkIfFileInfoIsSet();
return $this->xml_file_info->getRealPath() !== false;
}

public function getPathPart(string $pattern): string|null
{
$this->checkIfFileInfoIsSet();
$path_parts = explode(DIRECTORY_SEPARATOR, $this->getFilePath());
foreach ($path_parts as $path_part) {
if (preg_match($pattern, $path_part) === 1) {
Expand All @@ -118,7 +104,6 @@ public function getPathPart(string $pattern): string|null

public function pathContainsFolderName(string $folder_name): bool
{
$this->checkIfFileInfoIsSet();
$path_parts = explode(DIRECTORY_SEPARATOR, $this->getFilePath());
if (in_array($folder_name, $path_parts, true)) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,28 @@

namespace ImportHandler\I\File\XML\Export;

use ILIAS\Data\Version;
use ImportHandler\I\File\XML\ilHandlerInterface as ilXMLFileHandlerInterface;
use ImportHandler\I\File\XSD\ilHandlerInterface as ilXSDFileHandlerInterface;
use ImportHandler\I\File\XML\Node\Info\ilTreeInterface as ilXMLFileNodeInfoTreeInterface;
use ImportStatus\I\ilCollectionInterface as ilImportStatusCollectionInterface;
use SplFileInfo;

interface ilHandlerInterface extends ilXMLFileHandlerInterface
{
public function getXSDFileHandler(): ilXSDFileHandlerInterface;
public function getXSDFileHandler(): ilXSDFileHandlerInterface|null;

public function withFileInfo(SplFileInfo $file_info): ilHandlerInterface;
public function getVersion(): Version;

public function getType(): string;

public function getSubType(): string;

public function getILIASPath(ilXMLFileNodeInfoTreeInterface $component_tree): string;

public function withFileInfo(SplFileInfo $file_info): ilHandlerInterface;

public function loadExportInfo(): ilImportStatusCollectionInterface;

public function isContainerExportXML(): bool;
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public function withNumberingEnabled(bool $enabled): ilCollectionInterface;

public function toString(StatusType ...$types): string;

public function mergeContentToElements(
ilImportStatusContentHandlerInterface $content,
bool $at_front = true
): ilCollectionInterface;

/**
* @return ilImportStatusHandlerInterface[]
*/
Expand Down
53 changes: 39 additions & 14 deletions Services/Export/classes/ImportStatus/class.ilCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,30 @@

namespace ImportStatus;

use ImportStatus\I\Content\ilHandlerInterface as ilImportStatusContentHandlerInterface;
use ImportStatus\I\ilCollectionInterface;
use ImportStatus\I\ilHandlerInterface;
use ImportStatus\I\ilFactoryInterface as ilImportStatusFactoryInterface;

class ilCollection implements ilCollectionInterface
{
protected ilImportStatusFactoryInterface $status;
/**
* @var ilHandlerInterface[]
*/
protected array $status_collection;
protected array $elements;
protected int $index;
protected int $minIndex;
protected bool $is_numbering_enabled;

/**
* @param ilHandlerInterface[] $initial_values
*/
public function __construct(array $initial_values = [])
{
$this->status_collection = $initial_values;
public function __construct(
ilImportStatusFactoryInterface $status,
) {
$this->elements = [];
$this->minIndex = 0;
$this->index = $this->minIndex;
$this->is_numbering_enabled = false;
$this->status = $status;
}

/**
Expand All @@ -65,23 +67,27 @@ public function hasStatusType(StatusType $type): bool
public function withAddedStatus(ilHandlerInterface $import_status): ilCollection
{
$clone = clone $this;
$clone->status_collection[] = $import_status;
$clone->elements[] = $import_status;
return $clone;
}

public function getCollectionOfAllByType(StatusType $type): ilCollectionInterface
{
return new ilCollection($this->getArrayOfElementsWithType($type));
$clone = clone $this;
$clone->elements = $this->getArrayOfElementsWithType($type);
return $clone;
}

public function getMergedCollectionWith(ilCollectionInterface $other): ilCollectionInterface
{
return new ilCollection(array_merge($this->toArray(), $other->toArray()));
$clone = clone $this;
$clone->elements = array_merge($this->toArray(), $other->toArray());
return $clone;
}

public function current(): ilHandlerInterface
{
return $this->status_collection[$this->index];
return $this->elements[$this->index];
}

public function next(): void
Expand All @@ -106,15 +112,15 @@ public function rewind(): void

public function count(): int
{
return count($this->status_collection);
return count($this->elements);
}

/**
* @return ilHandlerInterface[]
*/
public function toArray(): array
{
return $this->status_collection;
return $this->elements;
}

public function withNumberingEnabled(bool $enabled): ilCollectionInterface
Expand All @@ -126,7 +132,7 @@ public function withNumberingEnabled(bool $enabled): ilCollectionInterface

public function toString(StatusType ...$types): string
{
$collection = new ilCollection();
$collection = $this->status->collection();
$msg = "<br>Listing status messages (of type(s)";
foreach ($types as $type) {
$msg .= " " . $type->name;
Expand All @@ -144,4 +150,23 @@ public function toString(StatusType ...$types): string
}
return $msg;
}

public function mergeContentToElements(
ilImportStatusContentHandlerInterface $content,
bool $at_front = true
): ilCollectionInterface {
$clone = clone $this;
$new_elements = [];
foreach ($clone->toArray() as $element) {
$new_elements[] = $at_front
? $this->status->handler()
->withType($element->getType())
->withContent($content->mergeWith($element->getContent()))
: $this->status->handler()
->withType($element->getType())
->withContent($element->getContent()->mergeWith($content));
}
$clone->elements = $new_elements;
return $clone;
}
}
2 changes: 1 addition & 1 deletion Services/Export/classes/ImportStatus/class.ilFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function handler(): ilImportStatusHandlerInterface

public function collection(): ilImportStatusHandlerCollectionInterface
{
return new ilImportStatusHandlerCollection();
return new ilImportStatusHandlerCollection($this);
}

public function exception(string $msg): ilImportStatusExceptionInterface
Expand Down
Loading

0 comments on commit c976920

Please sign in to comment.