diff options
author | Thomas Citharel <tcit@tcit.fr> | 2017-05-27 22:08:14 +0200 |
---|---|---|
committer | Thomas Citharel <tcit@tcit.fr> | 2017-05-27 22:08:14 +0200 |
commit | 6bc6fb1f60e7b81a21f844dca025671a2f4a4564 (patch) | |
tree | fde672650c6a2ef2ccb611a6a29989a7c944ea00 /src/Wallabag/CoreBundle/Helper/TagsAssigner.php | |
parent | 35941d57ee4d06ec3557d4b126d5f6fd263bcf3a (diff) | |
download | wallabag-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/Helper/TagsAssigner.php')
-rw-r--r-- | src/Wallabag/CoreBundle/Helper/TagsAssigner.php | 76 |
1 files changed, 76 insertions, 0 deletions
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 | |||
3 | namespace Wallabag\CoreBundle\Helper; | ||
4 | |||
5 | use Wallabag\CoreBundle\Entity\Entry; | ||
6 | use Wallabag\CoreBundle\Entity\Tag; | ||
7 | use Wallabag\CoreBundle\Repository\TagRepository; | ||
8 | |||
9 | class 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 | } | ||