From 6209cc7833f5e532f500db5b6055e1885564f2ec Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Tue, 14 Oct 2014 22:44:12 +0200 Subject: [PATCH] Refactored tag creating --- src/Services/TagService.php | 40 ++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/Services/TagService.php b/src/Services/TagService.php index a031727b..12f6c23e 100644 --- a/src/Services/TagService.php +++ b/src/Services/TagService.php @@ -99,33 +99,33 @@ class TagService return strtolower($tag->getName()); }; + $tagsNotToCreate = []; $tagNames = array_map($tagNameGetter, $tags); $tagNames = array_filter(array_unique($tagNames)); - - $tagsNotToCreate = $this->tagDao->findByNames($tagNames); - $tagNamesNotToCreate = array_map($tagNameGetter, $tagsNotToCreate); - $tagNamesToCreate = array_udiff($tagNames, $tagNamesNotToCreate, 'strcasecmp'); + foreach ($this->tagDao->findByNames($tagNames) as $tag) + $tagsNotToCreate[$tagNameGetter($tag)] = $tag; $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) { if (isset($tagsNotToCreate[$tagNameGetter($tag)])) - $tag = $tagsNotToCreate[$tagNameGetter($tag)]; - else - $tag = $createdTags[$tagNameGetter($tag)]; - $result[$key] = $tag; + continue; + + $tag->setCreationTime($this->timeService->getCurrentTime()); + $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; };