Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import: optimize validation loops to dictionary #532

Open
wants to merge 1 commit into
base: 4.x-dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 49 additions & 9 deletions API/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,27 @@ class Import
*/
private $accessValidator;

/**
* Map by id
*
* @var array
*/
protected $tagsMap = [];

/**
* Map by id
*
* @var array
*/
protected $triggersMap = [];

/**
* Map by id
*
* @var array
*/
protected $variablesMap = [];

public function __construct(Tag $tags, Trigger $triggers, Variable $variables, Container $containers, AccessValidator $accessValidator, TagsProvider $tagsProvider, TriggersProvider $triggersProvider, VariablesProvider $variablesProvider)
{
$this->tags = $tags;
Expand All @@ -72,6 +93,16 @@ public function __construct(Tag $tags, Trigger $triggers, Variable $variables, C
$this->tagsProvider = $tagsProvider;
$this->triggersProvider = $triggersProvider;
$this->variablesProvider = $variablesProvider;

foreach ($this->tagsProvider->getAllTags() as $tag) {
$this->tagsMap[$tag->getId()] = $tag;
}
foreach ($this->triggersProvider->getAllTriggers() as $trigger) {
$this->triggersMap[$trigger->getId()] = $trigger;
}
foreach ($this->variablesProvider->getAllVariables() as $variable) {
$this->variablesMap[$variable->getId()] = $variable;
}
}

public function checkImportContainerIsPossible($exportedContainerVersion, $idSite, $idContainer)
Expand All @@ -91,25 +122,34 @@ public function checkImportContainerIsPossible($exportedContainerVersion, $idSit
}

foreach ($exportedContainerVersion['tags'] as $tag) {
$this->tagsProvider->checkIsValidTag($tag['type']);

if ($this->tagsProvider->isCustomTemplate($tag['type'])) {
$type = $tag['type'];
$mtmTag = $this->tagsMap[$type] ?? null;
if (!$mtmTag) {
throw new \Exception(sprintf('The tag "%s" is not supported', $type));
}
if ($mtmTag->isCustomTemplate($type)) {
$this->accessValidator->checkUseCustomTemplatesCapability($idSite);
}
}

foreach ($exportedContainerVersion['triggers'] as $trigger) {
$this->triggersProvider->checkIsValidTrigger($trigger['type']);

if ($this->triggersProvider->isCustomTemplate($trigger['type'])) {
$type = $trigger['type'];
$mtmTrigger = $this->triggersMap[$type] ?? null;
if (!$mtmTrigger) {
throw new \Exception(sprintf('The trigger "%s" is not supported', $type));
}
if ($mtmTrigger->isCustomTemplate($type)) {
$this->accessValidator->checkUseCustomTemplatesCapability($idSite);
}
}

foreach ($exportedContainerVersion['variables'] as $variable) {
$this->variablesProvider->checkIsValidVariable($variable['type']);

if ($this->variablesProvider->isCustomTemplate($variable['type'])) {
$type = $variable['type'];
$mtmVariable = $this->variablesMap[$type] ?? null;
if (!$mtmVariable) {
throw new \Exception(sprintf('The variable "%s" is not supported', $type));
}
if ($mtmVariable->isCustomTemplate($type)) {
$this->accessValidator->checkUseCustomTemplatesCapability($idSite);
}
}
Expand Down