3 namespace Wallabag\CoreBundle\Controller
;
5 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route
;
6 use Symfony\Bundle\FrameworkBundle\Controller\Controller
;
7 use Symfony\Component\HttpFoundation\RedirectResponse
;
8 use Symfony\Component\Security\Core\Exception\AccessDeniedException
;
9 use Symfony\Component\Security\Core\Exception\InvalidArgumentException
;
10 use Wallabag\CoreBundle\Entity\Entry
;
11 use Wallabag\CoreBundle\Entity\Notification
;
12 use Wallabag\CoreBundle\Entity\Share
;
13 use Wallabag\CoreBundle\Event\EntrySavedEvent
;
14 use Wallabag\CoreBundle\Notifications\Action
;
15 use Wallabag\CoreBundle\Notifications\NoAction
;
16 use Wallabag\CoreBundle\Notifications\YesAction
;
17 use Wallabag\UserBundle\Entity\User
;
19 class ShareController
extends Controller
22 * @Route("/share-user/{entry}/{destination}", name="share-entry-user", requirements={"entry" = "\d+", "destination" = "\d+"})
24 * @param User $destination
25 * @throws AccessDeniedException
26 * @throws InvalidArgumentException
28 public function shareEntryAction(Entry
$entry, User
$destination)
31 if ($entry->getUser() !== $this->getUser()) {
32 throw new AccessDeniedException("You can't share this entry");
35 if ($destination === $this->getUser()) {
36 throw new InvalidArgumentException("You can't share entries to yourself");
40 $share->setUserOrigin($this->getUser())
42 ->setUserDestination($destination);
44 $em = $this->getDoctrine()->getManager();
48 $accept = new YesAction($this->generateUrl('share-entry-user-accept', ['share' => $share->getId()]));
50 $deny = new NoAction($this->generateUrl('share-entry-user-refuse', ['share' => $share->getId()]));
52 $notification = new Notification($destination);
53 $notification->setType(Notification
::TYPE_SHARE
)
54 ->setTitle($this->get('translator')->trans('share.notification.new.title'))
58 $em->persist($notification);
61 $this->redirectToRoute('view', ['id' => $entry->getId()]);
65 * @Route("/share-user/accept/{share}", name="share-entry-user-accept")
68 * @return RedirectResponse
69 * @throws AccessDeniedException
71 public function acceptShareAction(Share
$share)
73 if ($share->getUserDestination() !== $this->getUser()) {
74 throw new AccessDeniedException("You can't accept this entry");
77 $entry = new Entry($this->getUser());
78 $entry->setUrl($share->getEntry()->getUrl());
80 $em = $this->getDoctrine()->getManager();
82 if (false === $this->checkIfEntryAlreadyExists($entry)) {
83 $this->updateEntry($entry);
88 // entry saved, dispatch event about it!
89 $this->get('event_dispatcher')->dispatch(EntrySavedEvent
::NAME
, new EntrySavedEvent($entry));
93 $em->flush(); // we keep the previous flush above in case the event dispatcher would lead in using the saved entry
95 return $this->redirect($this->generateUrl('homepage'));
99 * @Route("/share-user/refuse/{share}", name="share-entry-user-refuse")
101 * @param Share $share
102 * @return RedirectResponse
104 public function refuseShareAction(Share
$share)
106 $em = $this->getDoctrine()->getManager();
110 return $this->redirect($this->generateUrl('homepage'));
114 * Fetch content and update entry.
115 * In case it fails, entry will return to avod loosing the data.
117 * @param Entry $entry
118 * @param string $prefixMessage Should be the translation key: entry_saved or entry_reloaded
122 private function updateEntry(Entry
$entry, $prefixMessage = 'entry_saved')
124 // put default title in case of fetching content failed
125 $entry->setTitle('No title found');
127 $message = 'flashes.entry.notice.'.$prefixMessage;
130 $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl());
131 } catch (\Exception
$e) {
132 $this->get('logger')->error('Error while saving an entry', [
137 $message = 'flashes.entry.notice.'.$prefixMessage.'_failed';
140 $this->get('session')->getFlashBag()->add('notice', $message);
146 * Check for existing entry, if it exists, redirect to it with a message.
148 * @param Entry $entry
152 private function checkIfEntryAlreadyExists(Entry
$entry)
154 return $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($entry->getUrl(), $this->getUser()->getId());