aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJeremy Benoist <jeremy.benoist@gmail.com>2017-06-07 15:07:55 +0200
committerJeremy Benoist <jeremy.benoist@gmail.com>2017-06-07 15:07:55 +0200
commitdb0c48af361eb20fbfcac625869de6f26112d3f7 (patch)
tree401b3a8e51cb6f0cb6b4d1f2c01ad45a26f6679e
parent645291e8feb0f3e977b9518da7d731fda8cf1f30 (diff)
downloadwallabag-db0c48af361eb20fbfcac625869de6f26112d3f7.tar.gz
wallabag-db0c48af361eb20fbfcac625869de6f26112d3f7.tar.zst
wallabag-db0c48af361eb20fbfcac625869de6f26112d3f7.zip
Refactorize the way to save an Entry in the API
-rw-r--r--src/Wallabag/ApiBundle/Controller/EntryRestController.php172
1 files changed, 70 insertions, 102 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php
index 7135e616..09b73ccb 100644
--- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php
+++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php
@@ -299,65 +299,18 @@ class EntryRestController extends WallabagRestController
299 $this->validateAuthentication(); 299 $this->validateAuthentication();
300 300
301 $url = $request->request->get('url'); 301 $url = $request->request->get('url');
302 $title = $request->request->get('title');
303 $tags = $request->request->get('tags', []);
304 $isArchived = $request->request->get('archive');
305 $isStarred = $request->request->get('starred');
306 $content = $request->request->get('content');
307 $language = $request->request->get('language');
308 $picture = $request->request->get('preview_picture');
309 $publishedAt = $request->request->get('published_at');
310 $authors = $request->request->get('authors', '');
311 302
312 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($url, $this->getUser()->getId()); 303 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId(
304 $url,
305 $this->getUser()->getId()
306 );
313 307
314 if (false === $entry) { 308 if (false === $entry) {
315 $entry = new Entry($this->getUser()); 309 $entry = new Entry($this->getUser());
316 }
317
318 try {
319 $this->get('wallabag_core.content_proxy')->updateEntry(
320 $entry,
321 $url,
322 [
323 'title' => $title,
324 'html' => $content,
325 'url' => $url,
326 'language' => $language,
327 'date' => $publishedAt,
328 // faking the preview picture
329 'open_graph' => [
330 'og_image' => $picture,
331 ],
332 'authors' => explode(',', $authors),
333 ]
334 );
335 } catch (\Exception $e) {
336 $this->get('logger')->error('Error while saving an entry', [
337 'exception' => $e,
338 'entry' => $entry,
339 ]);
340 $entry->setUrl($url); 310 $entry->setUrl($url);
341 } 311 }
342 312
343 if (!empty($tags)) { 313 $this->upsertEntry($entry, $request);
344 $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags);
345 }
346
347 if (!is_null($isStarred)) {
348 $entry->setStarred((bool) $isStarred);
349 }
350
351 if (!is_null($isArchived)) {
352 $entry->setArchived((bool) $isArchived);
353 }
354
355 $em = $this->getDoctrine()->getManager();
356 $em->persist($entry);
357 $em->flush();
358
359 // entry saved, dispatch event about it!
360 $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
361 314
362 return $this->sendResponse($entry); 315 return $this->sendResponse($entry);
363 } 316 }
@@ -389,56 +342,7 @@ class EntryRestController extends WallabagRestController
389 $this->validateAuthentication(); 342 $this->validateAuthentication();
390 $this->validateUserAccess($entry->getUser()->getId()); 343 $this->validateUserAccess($entry->getUser()->getId());
391 344
392 $title = $request->request->get('title'); 345 $this->upsertEntry($entry, $request, true);
393 $tags = $request->request->get('tags', '');
394 $isArchived = $request->request->get('archive');
395 $isStarred = $request->request->get('starred');
396 $content = $request->request->get('content');
397 $language = $request->request->get('language');
398 $picture = $request->request->get('preview_picture');
399 $publishedAt = $request->request->get('published_at');
400 $authors = $request->request->get('authors', '');
401
402 try {
403 $this->get('wallabag_core.content_proxy')->updateEntry(
404 $entry,
405 $entry->getUrl(),
406 [
407 'title' => !empty($title) ? $title : $entry->getTitle(),
408 'html' => !empty($content) ? $content : $entry->getContent(),
409 'url' => $entry->getUrl(),
410 'language' => $language,
411 'date' => $publishedAt,
412 // faking the preview picture
413 'open_graph' => [
414 'og_image' => $picture,
415 ],
416 'authors' => is_string($authors) ? explode(',', $authors) : [],
417 ],
418 // we don't want the content to be update by fetching the url
419 true
420 );
421 } catch (\Exception $e) {
422 $this->get('logger')->error('Error while saving an entry', [
423 'exception' => $e,
424 'entry' => $entry,
425 ]);
426 }
427
428 if (!is_null($isArchived)) {
429 $entry->setArchived((bool) $isArchived);
430 }
431
432 if (!is_null($isStarred)) {
433 $entry->setStarred((bool) $isStarred);
434 }
435
436 if (!empty($tags)) {
437 $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags);
438 }
439
440 $em = $this->getDoctrine()->getManager();
441 $em->flush();
442 346
443 return $this->sendResponse($entry); 347 return $this->sendResponse($entry);
444 } 348 }
@@ -705,4 +609,68 @@ class EntryRestController extends WallabagRestController
705 609
706 return (new JsonResponse())->setJson($json); 610 return (new JsonResponse())->setJson($json);
707 } 611 }
612
613 /**
614 * Update or Insert a new entry.
615 *
616 * @param Entry $entry
617 * @param Request $request
618 * @param bool $disableContentUpdate If we don't want the content to be update by fetching the url (used when patching instead of posting)
619 */
620 private function upsertEntry(Entry $entry, Request $request, $disableContentUpdate = false)
621 {
622 $title = $request->request->get('title');
623 $tags = $request->request->get('tags', []);
624 $isArchived = $request->request->get('archive');
625 $isStarred = $request->request->get('starred');
626 $content = $request->request->get('content');
627 $language = $request->request->get('language');
628 $picture = $request->request->get('preview_picture');
629 $publishedAt = $request->request->get('published_at');
630 $authors = $request->request->get('authors', '');
631
632 try {
633 $this->get('wallabag_core.content_proxy')->updateEntry(
634 $entry,
635 $entry->getUrl(),
636 [
637 'title' => !empty($title) ? $title : $entry->getTitle(),
638 'html' => !empty($content) ? $content : $entry->getContent(),
639 'url' => $entry->getUrl(),
640 'language' => !empty($language) ? $language : $entry->getLanguage(),
641 'date' => !empty($publishedAt) ? $publishedAt : $entry->getPublishedAt(),
642 // faking the open graph preview picture
643 'open_graph' => [
644 'og_image' => !empty($picture) ? $picture : $entry->getPreviewPicture(),
645 ],
646 'authors' => is_string($authors) ? explode(',', $authors) : $entry->getPublishedBy(),
647 ],
648 $disableContentUpdate
649 );
650 } catch (\Exception $e) {
651 $this->get('logger')->error('Error while saving an entry', [
652 'exception' => $e,
653 'entry' => $entry,
654 ]);
655 }
656
657 if (!is_null($isArchived)) {
658 $entry->setArchived((bool) $isArchived);
659 }
660
661 if (!is_null($isStarred)) {
662 $entry->setStarred((bool) $isStarred);
663 }
664
665 if (!empty($tags)) {
666 $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags);
667 }
668
669 $em = $this->getDoctrine()->getManager();
670 $em->persist($entry);
671 $em->flush();
672
673 // entry saved, dispatch event about it!
674 $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
675 }
708} 676}