]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/CoreBundle/Helper/ContentProxy.php
Enhance documentation and create a form to create a new client
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Helper / ContentProxy.php
index 3de8828f98e1e5740a155d60532d45c875010836..ba90b7310af83fd13774a1d87b38fb8fb241736a 100644 (file)
@@ -3,7 +3,11 @@
 namespace Wallabag\CoreBundle\Helper;
 
 use Graby\Graby;
+use Psr\Log\LoggerInterface as Logger;
 use Wallabag\CoreBundle\Entity\Entry;
+use Wallabag\CoreBundle\Entity\Tag;
+use Wallabag\CoreBundle\Tools\Utils;
+use Wallabag\CoreBundle\Repository\TagRepository;
 
 /**
  * This kind of proxy class take care of getting the content from an url
@@ -12,10 +16,16 @@ use Wallabag\CoreBundle\Entity\Entry;
 class ContentProxy
 {
     protected $graby;
+    protected $tagger;
+    protected $logger;
+    protected $tagRepository;
 
-    public function __construct(Graby $graby)
+    public function __construct(Graby $graby, RuleBasedTagger $tagger, TagRepository $tagRepository, Logger $logger)
     {
         $this->graby = $graby;
+        $this->tagger = $tagger;
+        $this->logger = $logger;
+        $this->tagRepository = $tagRepository;
     }
 
     /**
@@ -51,11 +61,56 @@ class ContentProxy
         $entry->setContent($html);
         $entry->setLanguage($content['language']);
         $entry->setMimetype($content['content_type']);
+        $entry->setReadingTime(Utils::getReadingTime($html));
+        $entry->setDomainName(parse_url($entry->getUrl(), PHP_URL_HOST));
 
         if (isset($content['open_graph']['og_image'])) {
             $entry->setPreviewPicture($content['open_graph']['og_image']);
         }
 
+        try {
+            $this->tagger->tag($entry);
+        } catch (\Exception $e) {
+            $this->logger->error('Error while trying to automatically tag an entry.', array(
+                'entry_url' => $url,
+                'error_msg' => $e->getMessage(),
+            ));
+        }
+
         return $entry;
     }
+
+    /**
+     * Assign some tags to an entry.
+     *
+     * @param Entry        $entry
+     * @param array|string $tags  An array of tag or a string coma separated of tag
+     */
+    public function assignTagsToEntry(Entry $entry, $tags)
+    {
+        if (!is_array($tags)) {
+            $tags = explode(',', $tags);
+        }
+
+        foreach ($tags as $label) {
+            $label = trim($label);
+
+            // avoid empty tag
+            if (0 === strlen($label)) {
+                continue;
+            }
+
+            $tagEntity = $this->tagRepository->findOneByLabel($label);
+
+            if (is_null($tagEntity)) {
+                $tagEntity = new Tag();
+                $tagEntity->setLabel($label);
+            }
+
+            // only add the tag on the entry if the relation doesn't exist
+            if (false === $entry->getTags()->contains($tagEntity)) {
+                $entry->addTag($tagEntity);
+            }
+        }
+    }
 }