aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Helper/ContentProxy.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/CoreBundle/Helper/ContentProxy.php')
-rw-r--r--src/Wallabag/CoreBundle/Helper/ContentProxy.php67
1 files changed, 14 insertions, 53 deletions
diff --git a/src/Wallabag/CoreBundle/Helper/ContentProxy.php b/src/Wallabag/CoreBundle/Helper/ContentProxy.php
index f222dd88..4b3e6fbb 100644
--- a/src/Wallabag/CoreBundle/Helper/ContentProxy.php
+++ b/src/Wallabag/CoreBundle/Helper/ContentProxy.php
@@ -5,9 +5,7 @@ namespace Wallabag\CoreBundle\Helper;
5use Graby\Graby; 5use Graby\Graby;
6use Psr\Log\LoggerInterface; 6use Psr\Log\LoggerInterface;
7use Wallabag\CoreBundle\Entity\Entry; 7use Wallabag\CoreBundle\Entity\Entry;
8use Wallabag\CoreBundle\Entity\Tag;
9use Wallabag\CoreBundle\Tools\Utils; 8use Wallabag\CoreBundle\Tools\Utils;
10use Wallabag\CoreBundle\Repository\TagRepository;
11use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeExtensionGuesser; 9use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeExtensionGuesser;
12 10
13/** 11/**
@@ -19,16 +17,15 @@ class ContentProxy
19 protected $graby; 17 protected $graby;
20 protected $tagger; 18 protected $tagger;
21 protected $logger; 19 protected $logger;
22 protected $tagRepository;
23 protected $mimeGuesser; 20 protected $mimeGuesser;
24 protected $fetchingErrorMessage; 21 protected $fetchingErrorMessage;
22 protected $eventDispatcher;
25 23
26 public function __construct(Graby $graby, RuleBasedTagger $tagger, TagRepository $tagRepository, LoggerInterface $logger, $fetchingErrorMessage) 24 public function __construct(Graby $graby, RuleBasedTagger $tagger, LoggerInterface $logger, $fetchingErrorMessage)
27 { 25 {
28 $this->graby = $graby; 26 $this->graby = $graby;
29 $this->tagger = $tagger; 27 $this->tagger = $tagger;
30 $this->logger = $logger; 28 $this->logger = $logger;
31 $this->tagRepository = $tagRepository;
32 $this->mimeGuesser = new MimeTypeExtensionGuesser(); 29 $this->mimeGuesser = new MimeTypeExtensionGuesser();
33 $this->fetchingErrorMessage = $fetchingErrorMessage; 30 $this->fetchingErrorMessage = $fetchingErrorMessage;
34 } 31 }
@@ -79,6 +76,18 @@ class ContentProxy
79 $entry->setContent($html); 76 $entry->setContent($html);
80 $entry->setHttpStatus(isset($content['status']) ? $content['status'] : ''); 77 $entry->setHttpStatus(isset($content['status']) ? $content['status'] : '');
81 78
79 if (isset($content['date']) && null !== $content['date'] && '' !== $content['date']) {
80 $entry->setPublishedAt(new \DateTime($content['date']));
81 }
82
83 if (!empty($content['authors'])) {
84 $entry->setPublishedBy($content['authors']);
85 }
86
87 if (!empty($content['all_headers'])) {
88 $entry->setHeaders($content['all_headers']);
89 }
90
82 $entry->setLanguage(isset($content['language']) ? $content['language'] : ''); 91 $entry->setLanguage(isset($content['language']) ? $content['language'] : '');
83 $entry->setMimetype(isset($content['content_type']) ? $content['content_type'] : ''); 92 $entry->setMimetype(isset($content['content_type']) ? $content['content_type'] : '');
84 $entry->setReadingTime(Utils::getReadingTime($html)); 93 $entry->setReadingTime(Utils::getReadingTime($html));
@@ -110,54 +119,6 @@ class ContentProxy
110 } 119 }
111 120
112 /** 121 /**
113 * Assign some tags to an entry.
114 *
115 * @param Entry $entry
116 * @param array|string $tags An array of tag or a string coma separated of tag
117 * @param array $entitiesReady Entities from the EntityManager which are persisted but not yet flushed
118 * It is mostly to fix duplicate tag on import @see http://stackoverflow.com/a/7879164/569101
119 */
120 public function assignTagsToEntry(Entry $entry, $tags, array $entitiesReady = [])
121 {
122 if (!is_array($tags)) {
123 $tags = explode(',', $tags);
124 }
125
126 // keeps only Tag entity from the "not yet flushed entities"
127 $tagsNotYetFlushed = [];
128 foreach ($entitiesReady as $entity) {
129 if ($entity instanceof Tag) {
130 $tagsNotYetFlushed[$entity->getLabel()] = $entity;
131 }
132 }
133
134 foreach ($tags as $label) {
135 $label = trim($label);
136
137 // avoid empty tag
138 if (0 === strlen($label)) {
139 continue;
140 }
141
142 if (isset($tagsNotYetFlushed[$label])) {
143 $tagEntity = $tagsNotYetFlushed[$label];
144 } else {
145 $tagEntity = $this->tagRepository->findOneByLabel($label);
146
147 if (is_null($tagEntity)) {
148 $tagEntity = new Tag();
149 $tagEntity->setLabel($label);
150 }
151 }
152
153 // only add the tag on the entry if the relation doesn't exist
154 if (false === $entry->getTags()->contains($tagEntity)) {
155 $entry->addTag($tagEntity);
156 }
157 }
158 }
159
160 /**
161 * Validate that the given content as enough value to be used 122 * Validate that the given content as enough value to be used
162 * instead of fetch the content from the url. 123 * instead of fetch the content from the url.
163 * 124 *