PreviewUrl.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /**
  3. * Copyright © 2015-2017 Ihor Vansach (ihor@magefan.com). All rights reserved.
  4. * See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
  5. *
  6. * Glory to Ukraine! Glory to the heroes!
  7. */
  8. namespace Magefan\Blog\Model;
  9. /**
  10. * Blog url model
  11. */
  12. class PreviewUrl extends Url
  13. {
  14. /**
  15. * Initialize dependencies.
  16. *
  17. * @param \Magento\Framework\Registry $registry
  18. * @param \Magento\Framework\Url $url
  19. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  20. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  21. */
  22. public function __construct(
  23. \Magento\Framework\Registry $registry,
  24. \Magento\Framework\Url $url,
  25. \Magento\Store\Model\StoreManagerInterface $storeManager,
  26. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  27. ) {
  28. parent::__construct($registry, $url, $storeManager, $scopeConfig);
  29. }
  30. /**
  31. * Retrieve blog page preview url
  32. * @param \Magento\Framework\Model\AbstractModel $object
  33. * @param string $controllerName
  34. * @return string
  35. */
  36. public function getUrl($object, $controllerName)
  37. {
  38. $storeIds = $object->getStoreIds();
  39. if (count($storeIds)) {
  40. $storeId = array_shift($storeIds);
  41. } else {
  42. $storeId = 0;
  43. }
  44. $this->storeId = $storeId;
  45. $scope = $this->_storeManager->getStore($this->storeId);
  46. $url = $this->_url->setScope($scope)
  47. ->getUrl('',
  48. [
  49. '_direct' => $this->getUrlPath($object->getIdentifier(), $controllerName),
  50. 'key' => null,
  51. '_nosid' => true,
  52. ]
  53. );
  54. $url .= (false === strpos($url, '?')) ? '?' : '&';
  55. $url .= 'secret=' . $object->getSecret();
  56. return $url;
  57. }
  58. }