3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+
6
7
declare (strict_types=1 );
7
8
8
9
namespace Magento \Downloadable \Controller \Download ;
9
10
11
+ use Magento \Downloadable \Controller \Download ;
10
12
use Magento \Downloadable \Helper \Download as DownloadHelper ;
13
+ use Magento \Downloadable \Helper \File ;
11
14
use Magento \Downloadable \Model \RelatedProductRetriever ;
12
15
use Magento \Downloadable \Model \Sample as SampleModel ;
16
+ use Magento \Downloadable \Model \SampleFactory ;
13
17
use Magento \Framework \App \Action \Context ;
18
+ use Magento \Catalog \Model \Product \Attribute \Source \Status ;
19
+ use Magento \CatalogInventory \Api \StockConfigurationInterface ;
20
+ use Magento \Framework \App \ObjectManager ;
14
21
use Magento \Framework \App \ResponseInterface ;
15
22
16
23
/**
17
24
* Class Sample executes download sample action.
18
25
*
19
26
* @SuppressWarnings(PHPMD.AllPurposeAction)
20
27
*/
21
- class Sample extends \ Magento \ Downloadable \ Controller \ Download
28
+ class Sample extends Download
22
29
{
23
30
/**
24
31
* @var RelatedProductRetriever
25
32
*/
26
33
private $ relatedProductRetriever ;
27
34
35
+ /**
36
+ * @var File
37
+ */
38
+ private $ file ;
39
+
40
+ /**
41
+ * @var SampleFactory
42
+ */
43
+ private $ sampleFactory ;
44
+
45
+ /**
46
+ * @var StockConfigurationInterface
47
+ */
48
+ private $ stockConfiguration ;
49
+
28
50
/**
29
51
* @param Context $context
30
52
* @param RelatedProductRetriever $relatedProductRetriever
53
+ * @param File|null $file
54
+ * @param SampleFactory|null $sampleFactory
55
+ * @param StockConfigurationInterface|null $stockConfiguration
31
56
*/
32
57
public function __construct (
33
58
Context $ context ,
34
- RelatedProductRetriever $ relatedProductRetriever
59
+ RelatedProductRetriever $ relatedProductRetriever ,
60
+ ?File $ file = null ,
61
+ ?SampleFactory $ sampleFactory = null ,
62
+ ?StockConfigurationInterface $ stockConfiguration = null
35
63
) {
36
64
parent ::__construct ($ context );
37
65
38
66
$ this ->relatedProductRetriever = $ relatedProductRetriever ;
67
+ $ this ->file = $ file ?: ObjectManager::getInstance ()->get (File::class);
68
+ $ this ->sampleFactory = $ sampleFactory ?: ObjectManager::getInstance ()->get (SampleFactory::class);
69
+ $ this ->stockConfiguration = $ stockConfiguration
70
+ ?: ObjectManager::getInstance ()->get (StockConfigurationInterface::class);
39
71
}
40
72
41
73
/**
@@ -47,43 +79,60 @@ public function execute()
47
79
{
48
80
$ sampleId = $ this ->getRequest ()->getParam ('sample_id ' , 0 );
49
81
/** @var SampleModel $sample */
50
- $ sample = $ this ->_objectManager ->create (SampleModel::class );
82
+ $ sample = $ this ->sampleFactory ->create ();
51
83
$ sample ->load ($ sampleId );
52
- if ($ sample ->getId () && $ this ->isProductSalable ($ sample )) {
53
- $ resource = '' ;
54
- $ resourceType = '' ;
55
- if ($ sample ->getSampleType () == DownloadHelper::LINK_TYPE_URL ) {
56
- $ resource = $ sample ->getSampleUrl ();
57
- $ resourceType = DownloadHelper::LINK_TYPE_URL ;
58
- } elseif ($ sample ->getSampleType () == DownloadHelper::LINK_TYPE_FILE ) {
59
- /** @var \Magento\Downloadable\Helper\File $helper */
60
- $ helper = $ this ->_objectManager ->get (\Magento \Downloadable \Helper \File::class);
61
- $ resource = $ helper ->getFilePath ($ sample ->getBasePath (), $ sample ->getSampleFile ());
62
- $ resourceType = DownloadHelper::LINK_TYPE_FILE ;
63
- }
64
- try {
65
- $ this ->_processDownload ($ resource , $ resourceType );
66
- // phpcs:ignore Magento2.Security.LanguageConstruct.ExitUsage
67
- exit (0 );
68
- } catch (\Exception $ e ) {
69
- $ this ->messageManager ->addError (
70
- __ ('Sorry, there was an error getting requested content. Please contact the store owner. ' )
71
- );
72
- }
84
+ if ($ this ->isCanDownload ($ sample )) {
85
+ $ this ->download ($ sample );
73
86
}
74
87
75
88
return $ this ->getResponse ()->setRedirect ($ this ->_redirect ->getRedirectUrl ());
76
89
}
77
90
78
91
/**
79
- * Check is related product salable.
92
+ * Is sample can be downloaded
80
93
*
81
94
* @param SampleModel $sample
82
95
* @return bool
83
96
*/
84
- private function isProductSalable (SampleModel $ sample ): bool
97
+ private function isCanDownload (SampleModel $ sample ): bool
85
98
{
86
99
$ product = $ this ->relatedProductRetriever ->getProduct ((int ) $ sample ->getProductId ());
87
- return $ product ? $ product ->isSalable () : false ;
100
+ if ($ product && $ sample ->getId ()) {
101
+ $ isProductEnabled = (int ) $ product ->getStatus () === Status::STATUS_ENABLED ;
102
+
103
+ return $ product ->isSalable () || $ this ->stockConfiguration ->isShowOutOfStock () && $ isProductEnabled ;
104
+ }
105
+
106
+ return false ;
107
+ }
108
+
109
+ /**
110
+ * Download process
111
+ *
112
+ * @param SampleModel $sample
113
+ * @return void
114
+ */
115
+ private function download (SampleModel $ sample ): void
116
+ {
117
+ $ resource = '' ;
118
+ $ resourceType = '' ;
119
+
120
+ if ($ sample ->getSampleType () === DownloadHelper::LINK_TYPE_URL ) {
121
+ $ resource = $ sample ->getSampleUrl ();
122
+ $ resourceType = DownloadHelper::LINK_TYPE_URL ;
123
+ } elseif ($ sample ->getSampleType () === DownloadHelper::LINK_TYPE_FILE ) {
124
+ $ resource = $ this ->file ->getFilePath ($ sample ->getBasePath (), $ sample ->getSampleFile ());
125
+ $ resourceType = DownloadHelper::LINK_TYPE_FILE ;
126
+ }
127
+
128
+ try {
129
+ $ this ->_processDownload ($ resource , $ resourceType );
130
+ // phpcs:ignore Magento2.Security.LanguageConstruct.ExitUsage
131
+ exit (0 );
132
+ } catch (\Exception $ e ) {
133
+ $ this ->messageManager ->addErrorMessage (
134
+ __ ('Sorry, there was an error getting requested content. Please contact the store owner. ' )
135
+ );
136
+ }
88
137
}
89
138
}
0 commit comments