]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php
Merge remote-tracking branch 'origin/master' into 2.3
[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;
0a033767 11use Psr\Log\LoggerInterface;
c3510620
KG
12
13class RuleBasedTagger
14{
15 private $rulerz;
16 private $tagRepository;
625acf33 17 private $entryRepository;
c3510620 18
0a033767 19 public function __construct(RulerZ $rulerz, TagRepository $tagRepository, EntryRepository $entryRepository, LoggerInterface $logger)
c3510620 20 {
347fa6be
NL
21 $this->rulerz = $rulerz;
22 $this->tagRepository = $tagRepository;
625acf33 23 $this->entryRepository = $entryRepository;
0a033767 24 $this->logger = $logger;
c3510620
KG
25 }
26
27 /**
28 * Add tags from rules defined by the user.
29 *
0cecfa25 30 * @param Entry $entry Entry to tag
c3510620
KG
31 */
32 public function tag(Entry $entry)
33 {
34 $rules = $this->getRulesForUser($entry->getUser());
35
36 foreach ($rules as $rule) {
ac9fec61 37 if (!$this->rulerz->satisfies($entry, $rule->getRule())) {
c3510620
KG
38 continue;
39 }
40
0a033767
NL
41 $this->logger->info('Matching rule.', [
42 'rule' => $rule->getRule(),
43 'tags' => $rule->getTags(),
44 ]);
45
ac9fec61 46 foreach ($rule->getTags() as $label) {
fc732227 47 $tag = $this->getTag($label);
c3510620
KG
48
49 $entry->addTag($tag);
50 }
51 }
52 }
53
625acf33
KG
54 /**
55 * Apply all the tagging rules defined by a user on its entries.
56 *
57 * @param User $user
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 *
110 * @param User $user
111 *
112 * @return array<TaggingRule>
113 */
c3510620
KG
114 private function getRulesForUser(User $user)
115 {
ac9fec61 116 return $user->getConfig()->getTaggingRules();
c3510620
KG
117 }
118}