X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2FWallabag%2FCoreBundle%2FHelper%2FRuleBasedTagger.php;h=b490e2090b4a95d5f263b1d1c5f9d17be8cdc9d3;hb=e4cf672ccf61689ba28c2e89fc55f83167800b18;hp=41ef25b8ce85f3e5df17088ef29601dfa45e3752;hpb=d25b8288216a09fa5cf7f40e614c133a6edd8a67;p=github%2Fwallabag%2Fwallabag.git diff --git a/src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php b/src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php index 41ef25b8..b490e209 100644 --- a/src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php +++ b/src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php @@ -25,7 +25,7 @@ class RuleBasedTagger /** * Add tags from rules defined by the user. * - * @param Entry $entry Entry to tag. + * @param Entry $entry Entry to tag */ public function tag(Entry $entry) { @@ -37,7 +37,7 @@ class RuleBasedTagger } foreach ($rule->getTags() as $label) { - $tag = $this->getTag($entry->getUser(), $label); + $tag = $this->getTag($label); $entry->addTag($tag); } @@ -49,12 +49,13 @@ class RuleBasedTagger * * @param User $user * - * @return array A list of modified entries. + * @return array A list of modified entries */ public function tagAllForUser(User $user) { $rules = $this->getRulesForUser($user); - $entries = array(); + $entries = []; + $tagsCache = []; foreach ($rules as $rule) { $qb = $this->entryRepository->getBuilderForAllByUser($user->getId()); @@ -62,7 +63,12 @@ class RuleBasedTagger foreach ($entries as $entry) { foreach ($rule->getTags() as $label) { - $tag = $this->getTag($user, $label); + // avoid new tag duplicate by manually caching them + if (!isset($tagsCache[$label])) { + $tagsCache[$label] = $this->getTag($label); + } + + $tag = $tagsCache[$label]; $entry->addTag($tag); } @@ -73,19 +79,18 @@ class RuleBasedTagger } /** - * Fetch a tag for a user. + * Fetch a tag. * - * @param User $user - * @param string $label The tag's label. + * @param string $label The tag's label * * @return Tag */ - private function getTag(User $user, $label) + private function getTag($label) { - $tag = $this->tagRepository->findOneByLabelAndUserId($label, $user->getId()); + $tag = $this->tagRepository->findOneByLabel($label); if (!$tag) { - $tag = new Tag($user); + $tag = new Tag(); $tag->setLabel($label); }