99
1010use Magento \Analytics \ReportXml \DB \ReportValidator ;
1111use Magento \Framework \Filesystem \Directory \WriteInterface ;
12+ use Magento \Framework \Filesystem \File \WriteInterface as FileWriteInterface ;
1213
1314/**
1415 * Writes reports in files in csv format
@@ -69,23 +70,7 @@ public function write(WriteInterface $directory, $path)
6970 continue ;
7071 }
7172 }
72- /** @var $providerObject */
73- $ providerObject = $ this ->providerFactory ->create ($ provider ['class ' ]);
74- $ fileName = $ provider ['parameters ' ] ? $ provider ['parameters ' ]['name ' ] : $ provider ['name ' ];
75- $ fileFullPath = $ path . $ fileName . '.csv ' ;
76- $ fileData = $ providerObject ->getReport (...array_values ($ provider ['parameters ' ]));
77- $ stream = $ directory ->openFile ($ fileFullPath , 'w+ ' );
78- $ stream ->lock ();
79- $ headers = [];
80- foreach ($ fileData as $ row ) {
81- if (!$ headers ) {
82- $ headers = array_keys ($ row );
83- $ stream ->writeCsv ($ headers );
84- }
85- $ stream ->writeCsv ($ this ->prepareRow ($ row ));
86- }
87- $ stream ->unlock ();
88- $ stream ->close ();
73+ $ this ->prepareData ($ provider , $ directory , $ path );
8974 }
9075 if ($ errorsList ) {
9176 $ errorStream = $ directory ->openFile ($ path . $ this ->errorsFileName , 'w+ ' );
@@ -100,6 +85,61 @@ public function write(WriteInterface $directory, $path)
10085 return true ;
10186 }
10287
88+ /**
89+ * Prepare report data
90+ *
91+ * @param array $provider
92+ * @param WriteInterface $directory
93+ * @param string $path
94+ * @return void
95+ * @throws \Magento\Framework\Exception\FileSystemException
96+ */
97+ private function prepareData (array $ provider , WriteInterface $ directory , string $ path )
98+ {
99+ /** @var $providerObject */
100+ $ providerObject = $ this ->providerFactory ->create ($ provider ['class ' ]);
101+ $ fileName = $ provider ['parameters ' ] ? $ provider ['parameters ' ]['name ' ] : $ provider ['name ' ];
102+ $ fileFullPath = $ path . $ fileName . '.csv ' ;
103+
104+ $ stream = $ directory ->openFile ($ fileFullPath , 'w+ ' );
105+ $ stream ->lock ();
106+
107+ $ headers = [];
108+ if ($ providerObject instanceof \Magento \Analytics \ReportXml \BatchReportProviderInterface) {
109+ $ fileData = $ providerObject ->getBatchReport (...array_values ($ provider ['parameters ' ]));
110+ do {
111+ $ this ->doWrite ($ fileData , $ stream , $ headers );
112+ $ fileData = $ providerObject ->getBatchReport (...array_values ($ provider ['parameters ' ]));
113+ $ fileData ->rewind ();
114+ } while ($ fileData ->valid ());
115+ } else {
116+ $ fileData = $ providerObject ->getReport (...array_values ($ provider ['parameters ' ]));
117+ $ this ->doWrite ($ fileData , $ stream , $ headers );
118+ }
119+
120+ $ stream ->unlock ();
121+ $ stream ->close ();
122+ }
123+
124+ /**
125+ * Write data to file
126+ *
127+ * @param \Traversable $fileData
128+ * @param FileWriteInterface $stream
129+ * @param array $headers
130+ * @return void
131+ */
132+ private function doWrite (\Traversable $ fileData , FileWriteInterface $ stream , array $ headers )
133+ {
134+ foreach ($ fileData as $ row ) {
135+ if (!$ headers ) {
136+ $ headers = array_keys ($ row );
137+ $ stream ->writeCsv ($ headers );
138+ }
139+ $ stream ->writeCsv ($ this ->prepareRow ($ row ));
140+ }
141+ }
142+
103143 /**
104144 * Replace wrong symbols in row
105145 *
0 commit comments