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