]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php
Enable PHPStan
[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 *
59 * @param User $user
60 *
0cecfa25 61 * @return array<Entry> A list of modified entries
625acf33
KG
62 */
63 public function tagAllForUser(User $user)
64 {
347fa6be 65 $rules = $this->getRulesForUser($user);
4094ea47 66 $entries = [];
b4fcd60e 67 $tagsCache = [];
625acf33
KG
68
69 foreach ($rules as $rule) {
347fa6be 70 $qb = $this->entryRepository->getBuilderForAllByUser($user->getId());
625acf33
KG
71 $entries = $this->rulerz->filter($qb, $rule->getRule());
72
73 foreach ($entries as $entry) {
74 foreach ($rule->getTags() as $label) {
b4fcd60e
JB
75 // avoid new tag duplicate by manually caching them
76 if (!isset($tagsCache[$label])) {
77 $tagsCache[$label] = $this->getTag($label);
78 }
79
80 $tag = $tagsCache[$label];
625acf33
KG
81
82 $entry->addTag($tag);
625acf33
KG
83 }
84 }
85 }
86
87 return $entries;
88 }
89
c3510620 90 /**
fc732227 91 * Fetch a tag.
c3510620 92 *
0cecfa25 93 * @param string $label The tag's label
c3510620
KG
94 *
95 * @return Tag
96 */
fc732227 97 private function getTag($label)
c3510620 98 {
fc732227 99 $tag = $this->tagRepository->findOneByLabel($label);
c3510620
KG
100
101 if (!$tag) {
fc732227 102 $tag = new Tag();
c3510620
KG
103 $tag->setLabel($label);
104 }
105
106 return $tag;
107 }
108
ac9fec61
KG
109 /**
110 * Retrieves the tagging rules for a given user.
111 *
112 * @param User $user
113 *
114 * @return array<TaggingRule>
115 */
c3510620
KG
116 private function getRulesForUser(User $user)
117 {
ac9fec61 118 return $user->getConfig()->getTaggingRules();
c3510620
KG
119 }
120}