src/Http/Controller/Api/V2/ActiveSession/ReplaceTabsAction.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Http\Controller\Api\V2\ActiveSession;
  3. use App\Database\Domain\Entity\Sessions\ActiveSessions\ActiveSessionTab;
  4. use App\Database\Domain\Repository\ActiveDeviceRepository;
  5. use App\Infrastructure\FileManager\FileSystem;
  6. use App\Infrastructure\Util\Identifier;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  13. /**
  14.  * @Route("/api/v2/active-session/devices/replace-links", name="api_v2_active_session_replace_tabs", methods={"PATCH"})
  15.  */
  16. class ReplaceTabsAction extends AbstractController
  17. {
  18.     public function __invoke(Request $requestActiveDeviceRepository $activeDeviceRepositoryEntityManagerInterface $emFileSystem $fileSystem)
  19.     {
  20.         $activeDevice $activeDeviceRepository->findOneBy(['deviceId' => $request->headers->get('BROWSER-ID'), 'user' => $this->getUser()]);
  21.         if (null === $activeDevice) {
  22.             throw new NotFoundHttpException('device was not found');
  23.         }
  24.         if ($activeDevice->getUser()->getId() !== $this->getUser()->getId()) {
  25.             throw new AccessDeniedException();
  26.         }
  27.         if (!$activeDevice->isSyncEnabled()) {
  28.             throw new AccessDeniedException('Sync is disabled for this device');
  29.         }
  30.         foreach ($activeDevice->getTabs() as $tab) {
  31.             $em->remove($tab);
  32.         }
  33.         $data json_decode($request->getContent(), true);
  34.         foreach ($data['links'] as $index => $newTab) {
  35.             $icon $newTab['icon'] ?? null;
  36.             if (empty($icon)) {
  37.                 $icons $fileSystem->getFilesList('/link-icons');
  38.                 $icon $_ENV['APP_HOST'].'/link-icons/'.$icons[rand(0count($icons) - 1)];
  39.             }
  40.             $tab = new ActiveSessionTab(
  41.                 Identifier::generate(),
  42.                 $activeDevice,
  43.                 $newTab['name'],
  44.                 $newTab['url'],
  45.                 $icon
  46.             );
  47.             $tab->setOrderIndex($index);
  48.             $tab->setIsPin($newTab['is_pin'] ?? false);
  49.             $em->persist($tab);
  50.         }
  51.         $em->flush();
  52.         return $this->json([]);
  53.     }
  54. }