예제 #1
0
 public function save(PostInterface $post)
 {
     $this->validate($post);
     $postData = $post->getData();
     $postModel = $this->_postFactory->create();
     $postModel->load($post->getId());
     $postModel->setData($postData);
     $postModel->save();
     $this->_postRegistry->push($postModel);
     $savedPost = $this->getByUrl($post->getUrlKey());
     $this->_eventManager->dispatch('post_save_after_data_object', ['post_data_object' => $savedPost, 'orig_post_data_object' => $post]);
     return $savedPost;
 }
예제 #2
0
 public function testRemoveByUrl()
 {
     $this->post->expects($this->exactly(2))->method('checkUrlKey')->willReturn(self::POST_ID);
     $this->post->expects($this->exactly(2))->method('load')->with(self::POST_ID)->willReturn($this->post);
     $this->post->expects($this->once())->method('getId')->willReturn(self::POST_ID);
     $this->postFactory->expects($this->exactly(2))->method('create')->willReturn($this->post);
     $this->post->expects($this->once())->method('getUrlKey')->willReturn(self::URL);
     $actual = $this->postRegistry->retrieveByUrl(self::URL);
     $this->assertSame($this->post, $actual);
     $this->postRegistry->removeByUrl(self::URL);
     $actual = $this->postRegistry->retrieveByUrl(self::URL);
     $this->assertSame($this->post, $actual);
 }
예제 #3
0
파일: Router.php 프로젝트: swnsma/practice
 /**
  * Validate and Match Blog Post and modify request
  *
  * @param RequestInterface $request
  * @return bool
  */
 public function match(RequestInterface $request)
 {
     $urlKey = str_replace("/blog/", "", $request->getPathInfo());
     $urlKey = rtrim($urlKey, '/');
     $urlKey = ltrim($urlKey, '/');
     /** @var \ISM\Blog\Model\Post $post */
     $post = $this->_postFactory->create();
     $id = $post->checkUrlKey($urlKey);
     if (!$id) {
         return null;
     }
     $request->setModuleName('blog')->setControllerName('view')->setActionName('index')->setParam('id', $id);
     $request->setAlias(Url::REWRITE_REQUEST_PATH_ALIAS, $urlKey);
     return $this->_actionFactory->create('Magento\\Framework\\App\\Action\\Forward');
 }
예제 #4
0
 public function retrieveByUrl($url)
 {
     if (isset($this->postRegistryByUrl[$url])) {
         return $this->postRegistryByUrl[$url];
     }
     /** @var $post Post $post */
     $post = $this->postFactory->create();
     $id = $post->checkUrlKey($url);
     if (!$id) {
         throw NoSuchEntityException::singleField('url_key', $url);
     } else {
         $post->load($id);
         $this->postRegistryById[$id] = $post;
         $this->postRegistryByUrl[$url] = $post;
         return $post;
     }
 }
예제 #5
0
파일: Delete.php 프로젝트: swnsma/practice
 /**
  * Execute post delete.
  *
  * @return $this
  */
 public function execute()
 {
     $id = $this->getRequest()->getParam('id');
     $post = $this->_postFactory->create();
     $resultRedirect = $this->resultRedirectFactory->create();
     $post->load($id);
     $img = $post->getImageUrl();
     $image = $this->_imageFactory->create();
     if (!$post->getId()) {
         return $resultRedirect->setPath('*/*/');
     }
     try {
         $post->delete();
     } catch (Exception $e) {
         $this->messageManager->addError($e->getMessage());
     }
     if ($img) {
         $image->remove(Upload::UPLOAD_POST_IMAGE_DIR . $img);
     }
     return $resultRedirect->setPath('*/*/');
 }