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