Refactored tag creating
This commit is contained in:
parent
8f03c68ec8
commit
6209cc7833
1 changed files with 20 additions and 20 deletions
|
@ -99,33 +99,33 @@ class TagService
|
||||||
return strtolower($tag->getName());
|
return strtolower($tag->getName());
|
||||||
};
|
};
|
||||||
|
|
||||||
|
$tagsNotToCreate = [];
|
||||||
$tagNames = array_map($tagNameGetter, $tags);
|
$tagNames = array_map($tagNameGetter, $tags);
|
||||||
$tagNames = array_filter(array_unique($tagNames));
|
$tagNames = array_filter(array_unique($tagNames));
|
||||||
|
foreach ($this->tagDao->findByNames($tagNames) as $tag)
|
||||||
$tagsNotToCreate = $this->tagDao->findByNames($tagNames);
|
$tagsNotToCreate[$tagNameGetter($tag)] = $tag;
|
||||||
$tagNamesNotToCreate = array_map($tagNameGetter, $tagsNotToCreate);
|
|
||||||
$tagNamesToCreate = array_udiff($tagNames, $tagNamesNotToCreate, 'strcasecmp');
|
|
||||||
|
|
||||||
$tagsToCreate = [];
|
$tagsToCreate = [];
|
||||||
foreach ($tagNamesToCreate as $tagName)
|
|
||||||
{
|
|
||||||
$tag = new Tag;
|
|
||||||
$tag->setName($tagName);
|
|
||||||
$tag->setCreationTime($this->timeService->getCurrentTime());
|
|
||||||
$tagsToCreate[] = $tag;
|
|
||||||
}
|
|
||||||
$createdTags = $this->tagDao->batchSave($tagsToCreate);
|
|
||||||
|
|
||||||
$tagsNotToCreate = array_combine($tagNamesNotToCreate, $tagsNotToCreate);
|
|
||||||
$createdTags = array_combine($tagNamesToCreate, $createdTags);
|
|
||||||
$result = [];
|
|
||||||
foreach ($tags as $key => $tag)
|
foreach ($tags as $key => $tag)
|
||||||
{
|
{
|
||||||
if (isset($tagsNotToCreate[$tagNameGetter($tag)]))
|
if (isset($tagsNotToCreate[$tagNameGetter($tag)]))
|
||||||
$tag = $tagsNotToCreate[$tagNameGetter($tag)];
|
continue;
|
||||||
else
|
|
||||||
$tag = $createdTags[$tagNameGetter($tag)];
|
$tag->setCreationTime($this->timeService->getCurrentTime());
|
||||||
$result[$key] = $tag;
|
$tagsToCreate[$tagNameGetter($tag)] = $tag;
|
||||||
|
}
|
||||||
|
|
||||||
|
$createdTags = [];
|
||||||
|
foreach ($this->tagDao->batchSave($tagsToCreate) as $tag)
|
||||||
|
$createdTags[$tagNameGetter($tag)] = $tag;
|
||||||
|
|
||||||
|
$result = [];
|
||||||
|
foreach ($tags as $key => $tag)
|
||||||
|
{
|
||||||
|
$result[$key] =
|
||||||
|
isset($tagsToCreate[$tagNameGetter($tag)])
|
||||||
|
? $createdTags[$tagNameGetter($tag)]
|
||||||
|
: $tagsNotToCreate[$tagNameGetter($tag)];
|
||||||
}
|
}
|
||||||
return $result;
|
return $result;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue