bookmarkRepository = $bookmarkRepository; $this->bookmarkManagement = $bookmarkManagement; $this->bookmarkFactory = $bookmarkFactory; $this->userContext = $userContext; $this->jsonDecoder = $jsonDecoder; $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance() ->get(\Magento\Framework\Serialize\Serializer\Json::class); } /** * Action for AJAX request * * @return void * @throws \InvalidArgumentException * @throws \LogicException */ public function execute() { $bookmark = $this->bookmarkFactory->create(); $jsonData = $this->_request->getParam('data'); if (!$jsonData) { throw new \InvalidArgumentException('Invalid parameter "data"'); } $data = $this->serializer->unserialize($jsonData); $action = key($data); switch ($action) { case self::ACTIVE_IDENTIFIER: $this->updateCurrentBookmark($data[$action]); break; case self::CURRENT_IDENTIFIER: $this->updateBookmark( $bookmark, $action, $bookmark->getTitle(), $jsonData ); break; case self::VIEWS_IDENTIFIER: foreach ($data[$action] as $identifier => $data) { $this->updateBookmark( $bookmark, $identifier, isset($data['label']) ? $data['label'] : '', $jsonData ); $this->updateCurrentBookmark($identifier); } break; default: throw new \LogicException(__('Unsupported bookmark action.')); } } /** * Update bookmarks based on request params * * @param BookmarkInterface $bookmark * @param string $identifier * @param string $title * @param string $config * @return void */ protected function updateBookmark(BookmarkInterface $bookmark, $identifier, $title, $config) { $updateBookmark = $this->checkBookmark($identifier); if ($updateBookmark !== false) { $bookmark = $updateBookmark; } $bookmark->setUserId($this->userContext->getUserId()) ->setNamespace($this->_request->getParam('namespace')) ->setIdentifier($identifier) ->setTitle($title) ->setConfig($config); $this->bookmarkRepository->save($bookmark); } /** * Update current bookmark * * @param string $identifier * @return void */ protected function updateCurrentBookmark($identifier) { $bookmarks = $this->bookmarkManagement->loadByNamespace($this->_request->getParam('namespace')); foreach ($bookmarks->getItems() as $bookmark) { if ($bookmark->getIdentifier() == $identifier) { $bookmark->setCurrent(true); } else { $bookmark->setCurrent(false); } $this->bookmarkRepository->save($bookmark); } } /** * Check bookmark by identifier * * @param string $identifier * @return bool|BookmarkInterface */ protected function checkBookmark($identifier) { $result = false; $updateBookmark = $this->bookmarkManagement->getByIdentifierNamespace( $identifier, $this->_request->getParam('namespace') ); if ($updateBookmark) { $result = $updateBookmark; } return $result; } }