diff options
Diffstat (limited to 'src/Wallabag/CoreBundle/Helper/ContentProxy.php')
-rw-r--r-- | src/Wallabag/CoreBundle/Helper/ContentProxy.php | 40 |
1 files changed, 39 insertions, 1 deletions
diff --git a/src/Wallabag/CoreBundle/Helper/ContentProxy.php b/src/Wallabag/CoreBundle/Helper/ContentProxy.php index bd8b993a..ba90b731 100644 --- a/src/Wallabag/CoreBundle/Helper/ContentProxy.php +++ b/src/Wallabag/CoreBundle/Helper/ContentProxy.php | |||
@@ -5,7 +5,9 @@ namespace Wallabag\CoreBundle\Helper; | |||
5 | use Graby\Graby; | 5 | use Graby\Graby; |
6 | use Psr\Log\LoggerInterface as Logger; | 6 | use Psr\Log\LoggerInterface as Logger; |
7 | use Wallabag\CoreBundle\Entity\Entry; | 7 | use Wallabag\CoreBundle\Entity\Entry; |
8 | use Wallabag\CoreBundle\Entity\Tag; | ||
8 | use Wallabag\CoreBundle\Tools\Utils; | 9 | use Wallabag\CoreBundle\Tools\Utils; |
10 | use Wallabag\CoreBundle\Repository\TagRepository; | ||
9 | 11 | ||
10 | /** | 12 | /** |
11 | * This kind of proxy class take care of getting the content from an url | 13 | * This kind of proxy class take care of getting the content from an url |
@@ -16,12 +18,14 @@ class ContentProxy | |||
16 | protected $graby; | 18 | protected $graby; |
17 | protected $tagger; | 19 | protected $tagger; |
18 | protected $logger; | 20 | protected $logger; |
21 | protected $tagRepository; | ||
19 | 22 | ||
20 | public function __construct(Graby $graby, RuleBasedTagger $tagger, Logger $logger) | 23 | public function __construct(Graby $graby, RuleBasedTagger $tagger, TagRepository $tagRepository, Logger $logger) |
21 | { | 24 | { |
22 | $this->graby = $graby; | 25 | $this->graby = $graby; |
23 | $this->tagger = $tagger; | 26 | $this->tagger = $tagger; |
24 | $this->logger = $logger; | 27 | $this->logger = $logger; |
28 | $this->tagRepository = $tagRepository; | ||
25 | } | 29 | } |
26 | 30 | ||
27 | /** | 31 | /** |
@@ -75,4 +79,38 @@ class ContentProxy | |||
75 | 79 | ||
76 | return $entry; | 80 | return $entry; |
77 | } | 81 | } |
82 | |||
83 | /** | ||
84 | * Assign some tags to an entry. | ||
85 | * | ||
86 | * @param Entry $entry | ||
87 | * @param array|string $tags An array of tag or a string coma separated of tag | ||
88 | */ | ||
89 | public function assignTagsToEntry(Entry $entry, $tags) | ||
90 | { | ||
91 | if (!is_array($tags)) { | ||
92 | $tags = explode(',', $tags); | ||
93 | } | ||
94 | |||
95 | foreach ($tags as $label) { | ||
96 | $label = trim($label); | ||
97 | |||
98 | // avoid empty tag | ||
99 | if (0 === strlen($label)) { | ||
100 | continue; | ||
101 | } | ||
102 | |||
103 | $tagEntity = $this->tagRepository->findOneByLabel($label); | ||
104 | |||
105 | if (is_null($tagEntity)) { | ||
106 | $tagEntity = new Tag(); | ||
107 | $tagEntity->setLabel($label); | ||
108 | } | ||
109 | |||
110 | // only add the tag on the entry if the relation doesn't exist | ||
111 | if (false === $entry->getTags()->contains($tagEntity)) { | ||
112 | $entry->addTag($tagEntity); | ||
113 | } | ||
114 | } | ||
115 | } | ||
78 | } | 116 | } |