]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php
Avoid tag duplication when tagging all articles
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Helper / RuleBasedTagger.php
CommitLineData
c3510620
KG
1<?php
2
3namespace Wallabag\CoreBundle\Helper;
4
5use RulerZ\RulerZ;
c3510620
KG
6use Wallabag\CoreBundle\Entity\Entry;
7use Wallabag\CoreBundle\Entity\Tag;
625acf33 8use Wallabag\CoreBundle\Repository\EntryRepository;
c3510620
KG
9use Wallabag\CoreBundle\Repository\TagRepository;
10use Wallabag\UserBundle\Entity\User;
11
12class RuleBasedTagger
13{
14 private $rulerz;
15 private $tagRepository;
625acf33 16 private $entryRepository;
c3510620 17
625acf33 18 public function __construct(RulerZ $rulerz, TagRepository $tagRepository, EntryRepository $entryRepository)
c3510620 19 {
347fa6be
NL
20 $this->rulerz = $rulerz;
21 $this->tagRepository = $tagRepository;
625acf33 22 $this->entryRepository = $entryRepository;
c3510620
KG
23 }
24
25 /**
26 * Add tags from rules defined by the user.
27 *
0cecfa25 28 * @param Entry $entry Entry to tag
c3510620
KG
29 */
30 public function tag(Entry $entry)
31 {
32 $rules = $this->getRulesForUser($entry->getUser());
33
34 foreach ($rules as $rule) {
ac9fec61 35 if (!$this->rulerz->satisfies($entry, $rule->getRule())) {
c3510620
KG
36 continue;
37 }
38
ac9fec61 39 foreach ($rule->getTags() as $label) {
fc732227 40 $tag = $this->getTag($label);
c3510620
KG
41
42 $entry->addTag($tag);
43 }
44 }
45 }
46
625acf33
KG
47 /**
48 * Apply all the tagging rules defined by a user on its entries.
49 *
50 * @param User $user
51 *
0cecfa25 52 * @return array<Entry> A list of modified entries
625acf33
KG
53 */
54 public function tagAllForUser(User $user)
55 {
347fa6be 56 $rules = $this->getRulesForUser($user);
4094ea47 57 $entries = [];
b4fcd60e 58 $tagsCache = [];
625acf33
KG
59
60 foreach ($rules as $rule) {
347fa6be 61 $qb = $this->entryRepository->getBuilderForAllByUser($user->getId());
625acf33
KG
62 $entries = $this->rulerz->filter($qb, $rule->getRule());
63
64 foreach ($entries as $entry) {
65 foreach ($rule->getTags() as $label) {
b4fcd60e
JB
66 // avoid new tag duplicate by manually caching them
67 if (!isset($tagsCache[$label])) {
68 $tagsCache[$label] = $this->getTag($label);
69 }
70
71 $tag = $tagsCache[$label];
625acf33
KG
72
73 $entry->addTag($tag);
625acf33
KG
74 }
75 }
76 }
77
78 return $entries;
79 }
80
c3510620 81 /**
fc732227 82 * Fetch a tag.
c3510620 83 *
0cecfa25 84 * @param string $label The tag's label
c3510620
KG
85 *
86 * @return Tag
87 */
fc732227 88 private function getTag($label)
c3510620 89 {
fc732227 90 $tag = $this->tagRepository->findOneByLabel($label);
c3510620
KG
91
92 if (!$tag) {
fc732227 93 $tag = new Tag();
c3510620
KG
94 $tag->setLabel($label);
95 }
96
97 return $tag;
98 }
99
ac9fec61
KG
100 /**
101 * Retrieves the tagging rules for a given user.
102 *
103 * @param User $user
104 *
105 * @return array<TaggingRule>
106 */
c3510620
KG
107 private function getRulesForUser(User $user)
108 {
ac9fec61 109 return $user->getConfig()->getTaggingRules();
c3510620
KG
110 }
111}