aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle
diff options
context:
space:
mode:
authorThomas Citharel <tcit@tcit.fr>2017-05-27 22:08:14 +0200
committerThomas Citharel <tcit@tcit.fr>2017-05-27 22:08:14 +0200
commit6bc6fb1f60e7b81a21f844dca025671a2f4a4564 (patch)
treefde672650c6a2ef2ccb611a6a29989a7c944ea00 /src/Wallabag/CoreBundle
parent35941d57ee4d06ec3557d4b126d5f6fd263bcf3a (diff)
downloadwallabag-6bc6fb1f60e7b81a21f844dca025671a2f4a4564.tar.gz
wallabag-6bc6fb1f60e7b81a21f844dca025671a2f4a4564.tar.zst
wallabag-6bc6fb1f60e7b81a21f844dca025671a2f4a4564.zip
Move Tags assigner to a separate file
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
Diffstat (limited to 'src/Wallabag/CoreBundle')
-rw-r--r--src/Wallabag/CoreBundle/Controller/TagController.php2
-rw-r--r--src/Wallabag/CoreBundle/Helper/ContentProxy.php56
-rw-r--r--src/Wallabag/CoreBundle/Helper/TagsAssigner.php76
-rw-r--r--src/Wallabag/CoreBundle/Resources/config/services.yml6
4 files changed, 85 insertions, 55 deletions
diff --git a/src/Wallabag/CoreBundle/Controller/TagController.php b/src/Wallabag/CoreBundle/Controller/TagController.php
index 8a093289..fb6a720b 100644
--- a/src/Wallabag/CoreBundle/Controller/TagController.php
+++ b/src/Wallabag/CoreBundle/Controller/TagController.php
@@ -28,7 +28,7 @@ class TagController extends Controller
28 $form->handleRequest($request); 28 $form->handleRequest($request);
29 29
30 if ($form->isSubmitted() && $form->isValid()) { 30 if ($form->isSubmitted() && $form->isValid()) {
31 $this->get('wallabag_core.content_proxy')->assignTagsToEntry( 31 $this->get('wallabag_core.tags_assigner')->assignTagsToEntry(
32 $entry, 32 $entry,
33 $form->get('label')->getData() 33 $form->get('label')->getData()
34 ); 34 );
diff --git a/src/Wallabag/CoreBundle/Helper/ContentProxy.php b/src/Wallabag/CoreBundle/Helper/ContentProxy.php
index 9a08db3d..076135c7 100644
--- a/src/Wallabag/CoreBundle/Helper/ContentProxy.php
+++ b/src/Wallabag/CoreBundle/Helper/ContentProxy.php
@@ -4,10 +4,9 @@ namespace Wallabag\CoreBundle\Helper;
4 4
5use Graby\Graby; 5use Graby\Graby;
6use Psr\Log\LoggerInterface; 6use Psr\Log\LoggerInterface;
7use Symfony\Component\EventDispatcher\EventDispatcher;
7use Wallabag\CoreBundle\Entity\Entry; 8use Wallabag\CoreBundle\Entity\Entry;
8use Wallabag\CoreBundle\Entity\Tag;
9use Wallabag\CoreBundle\Tools\Utils; 9use Wallabag\CoreBundle\Tools\Utils;
10use Wallabag\CoreBundle\Repository\TagRepository;
11use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeExtensionGuesser; 10use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeExtensionGuesser;
12 11
13/** 12/**
@@ -19,16 +18,15 @@ class ContentProxy
19 protected $graby; 18 protected $graby;
20 protected $tagger; 19 protected $tagger;
21 protected $logger; 20 protected $logger;
22 protected $tagRepository;
23 protected $mimeGuesser; 21 protected $mimeGuesser;
24 protected $fetchingErrorMessage; 22 protected $fetchingErrorMessage;
23 protected $eventDispatcher;
25 24
26 public function __construct(Graby $graby, RuleBasedTagger $tagger, TagRepository $tagRepository, LoggerInterface $logger, $fetchingErrorMessage) 25 public function __construct(Graby $graby, RuleBasedTagger $tagger, LoggerInterface $logger, $fetchingErrorMessage)
27 { 26 {
28 $this->graby = $graby; 27 $this->graby = $graby;
29 $this->tagger = $tagger; 28 $this->tagger = $tagger;
30 $this->logger = $logger; 29 $this->logger = $logger;
31 $this->tagRepository = $tagRepository;
32 $this->mimeGuesser = new MimeTypeExtensionGuesser(); 30 $this->mimeGuesser = new MimeTypeExtensionGuesser();
33 $this->fetchingErrorMessage = $fetchingErrorMessage; 31 $this->fetchingErrorMessage = $fetchingErrorMessage;
34 } 32 }
@@ -122,54 +120,6 @@ class ContentProxy
122 } 120 }
123 121
124 /** 122 /**
125 * Assign some tags to an entry.
126 *
127 * @param Entry $entry
128 * @param array|string $tags An array of tag or a string coma separated of tag
129 * @param array $entitiesReady Entities from the EntityManager which are persisted but not yet flushed
130 * It is mostly to fix duplicate tag on import @see http://stackoverflow.com/a/7879164/569101
131 */
132 public function assignTagsToEntry(Entry $entry, $tags, array $entitiesReady = [])
133 {
134 if (!is_array($tags)) {
135 $tags = explode(',', $tags);
136 }
137
138 // keeps only Tag entity from the "not yet flushed entities"
139 $tagsNotYetFlushed = [];
140 foreach ($entitiesReady as $entity) {
141 if ($entity instanceof Tag) {
142 $tagsNotYetFlushed[$entity->getLabel()] = $entity;
143 }
144 }
145
146 foreach ($tags as $label) {
147 $label = trim($label);
148
149 // avoid empty tag
150 if (0 === strlen($label)) {
151 continue;
152 }
153
154 if (isset($tagsNotYetFlushed[$label])) {
155 $tagEntity = $tagsNotYetFlushed[$label];
156 } else {
157 $tagEntity = $this->tagRepository->findOneByLabel($label);
158
159 if (is_null($tagEntity)) {
160 $tagEntity = new Tag();
161 $tagEntity->setLabel($label);
162 }
163 }
164
165 // only add the tag on the entry if the relation doesn't exist
166 if (false === $entry->getTags()->contains($tagEntity)) {
167 $entry->addTag($tagEntity);
168 }
169 }
170 }
171
172 /**
173 * Validate that the given content as enough value to be used 123 * Validate that the given content as enough value to be used
174 * instead of fetch the content from the url. 124 * instead of fetch the content from the url.
175 * 125 *
diff --git a/src/Wallabag/CoreBundle/Helper/TagsAssigner.php b/src/Wallabag/CoreBundle/Helper/TagsAssigner.php
new file mode 100644
index 00000000..ae712d77
--- /dev/null
+++ b/src/Wallabag/CoreBundle/Helper/TagsAssigner.php
@@ -0,0 +1,76 @@
1<?php
2
3namespace Wallabag\CoreBundle\Helper;
4
5use Wallabag\CoreBundle\Entity\Entry;
6use Wallabag\CoreBundle\Entity\Tag;
7use Wallabag\CoreBundle\Repository\TagRepository;
8
9class TagsAssigner
10{
11
12 /**
13 * @var TagRepository $tagRepository
14 */
15 protected $tagRepository;
16
17 public function __construct(TagRepository $tagRepository)
18 {
19 $this->tagRepository = $tagRepository;
20 }
21
22 /**
23 * Assign some tags to an entry.
24 *
25 * @param Entry $entry
26 * @param array|string $tags An array of tag or a string coma separated of tag
27 * @param array $entitiesReady Entities from the EntityManager which are persisted but not yet flushed
28 * It is mostly to fix duplicate tag on import @see http://stackoverflow.com/a/7879164/569101
29 *
30 * @return Tag[]
31 */
32 public function assignTagsToEntry(Entry $entry, $tags, array $entitiesReady = [])
33 {
34 $tagsEntities = [];
35
36 if (!is_array($tags)) {
37 $tags = explode(',', $tags);
38 }
39
40 // keeps only Tag entity from the "not yet flushed entities"
41 $tagsNotYetFlushed = [];
42 foreach ($entitiesReady as $entity) {
43 if ($entity instanceof Tag) {
44 $tagsNotYetFlushed[$entity->getLabel()] = $entity;
45 }
46 }
47
48 foreach ($tags as $label) {
49 $label = trim($label);
50
51 // avoid empty tag
52 if (0 === strlen($label)) {
53 continue;
54 }
55
56 if (isset($tagsNotYetFlushed[$label])) {
57 $tagEntity = $tagsNotYetFlushed[$label];
58 } else {
59 $tagEntity = $this->tagRepository->findOneByLabel($label);
60
61 if (null === $tagEntity) {
62 $tagEntity = new Tag();
63 $tagEntity->setLabel($label);
64 }
65 }
66
67 // only add the tag on the entry if the relation doesn't exist
68 if (false === $entry->getTags()->contains($tagEntity)) {
69 $entry->addTag($tagEntity);
70 $tagsEntities[] = $tagEntity;
71 }
72 }
73
74 return $tagsEntities;
75 }
76}
diff --git a/src/Wallabag/CoreBundle/Resources/config/services.yml b/src/Wallabag/CoreBundle/Resources/config/services.yml
index a9134ac3..a68b2fdc 100644
--- a/src/Wallabag/CoreBundle/Resources/config/services.yml
+++ b/src/Wallabag/CoreBundle/Resources/config/services.yml
@@ -89,10 +89,14 @@ services:
89 arguments: 89 arguments:
90 - "@wallabag_core.graby" 90 - "@wallabag_core.graby"
91 - "@wallabag_core.rule_based_tagger" 91 - "@wallabag_core.rule_based_tagger"
92 - "@wallabag_core.tag_repository"
93 - "@logger" 92 - "@logger"
94 - '%wallabag_core.fetching_error_message%' 93 - '%wallabag_core.fetching_error_message%'
95 94
95 wallabag_core.tags_assigner:
96 class: Wallabag\CoreBundle\Helper\TagsAssigner
97 arguments:
98 - "@wallabag_core.tag_repository"
99
96 wallabag_core.rule_based_tagger: 100 wallabag_core.rule_based_tagger:
97 class: Wallabag\CoreBundle\Helper\RuleBasedTagger 101 class: Wallabag\CoreBundle\Helper\RuleBasedTagger
98 arguments: 102 arguments: