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