LinkProvider.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Analytics\Model;
  7. use Magento\Analytics\Api\Data\LinkInterfaceFactory;
  8. use Magento\Analytics\Api\LinkProviderInterface;
  9. use Magento\Framework\Exception\NoSuchEntityException;
  10. use Magento\Framework\UrlInterface;
  11. use Magento\Store\Model\StoreManagerInterface;
  12. /**
  13. * Provides link to file with collected report data.
  14. */
  15. class LinkProvider implements LinkProviderInterface
  16. {
  17. /**
  18. * @var LinkInterfaceFactory
  19. */
  20. private $linkFactory;
  21. /**
  22. * @var FileInfoManager
  23. */
  24. private $fileInfoManager;
  25. /**
  26. * @var StoreManagerInterface
  27. */
  28. private $storeManager;
  29. /**
  30. * @param LinkInterfaceFactory $linkInterfaceFactory
  31. * @param FileInfoManager $fileInfoManager
  32. * @param StoreManagerInterface $storeManager
  33. */
  34. public function __construct(
  35. LinkInterfaceFactory $linkFactory,
  36. FileInfoManager $fileInfoManager,
  37. StoreManagerInterface $storeManager
  38. ) {
  39. $this->linkFactory = $linkFactory;
  40. $this->fileInfoManager = $fileInfoManager;
  41. $this->storeManager = $storeManager;
  42. }
  43. /**
  44. * Returns base url to file according to store configuration
  45. *
  46. * @param FileInfo $fileInfo
  47. * @return string
  48. */
  49. private function getBaseUrl(FileInfo $fileInfo)
  50. {
  51. return $this->storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_MEDIA) . $fileInfo->getPath();
  52. }
  53. /**
  54. * Verify is requested file ready
  55. *
  56. * @param FileInfo $fileInfo
  57. * @return bool
  58. */
  59. private function isFileReady(FileInfo $fileInfo)
  60. {
  61. return $fileInfo->getPath() && $fileInfo->getInitializationVector();
  62. }
  63. /**
  64. * @inheritdoc
  65. */
  66. public function get()
  67. {
  68. $fileInfo = $this->fileInfoManager->load();
  69. if (!$this->isFileReady($fileInfo)) {
  70. throw new NoSuchEntityException(__('File is not ready yet.'));
  71. }
  72. return $this->linkFactory->create(
  73. [
  74. 'url' => $this->getBaseUrl($fileInfo),
  75. 'initializationVector' => base64_encode($fileInfo->getInitializationVector())
  76. ]
  77. );
  78. }
  79. }