Skip to content

Commit c408a81

Browse files
committed
MDEE-376: create repository for reading exported orders and extract exporter.
1 parent 000a448 commit c408a81

3 files changed

Lines changed: 128 additions & 0 deletions

File tree

SalesOrdersDataExporter/Console/Command/ExportOnDemand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
/**
2424
* Command export orders since certain time in the past
25+
* @deprecated will be removed
2526
*/
2627
class ExportOnDemand extends Command
2728
{
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\SalesOrdersDataExporter\Model;
9+
10+
use Generator;
11+
use Magento\DataExporter\Model\Indexer\FeedIndexMetadata;
12+
use Magento\Framework\App\ResourceConnection;
13+
14+
class OnDemandExportOrdersRepository
15+
{
16+
private FeedIndexMetadata $metadata;
17+
private ResourceConnection $resourceConnection;
18+
private BatchIteratorFactory $batchIteratorFactory;
19+
20+
public function __construct(
21+
FeedIndexMetadata $metadata,
22+
ResourceConnection $resourceConnection,
23+
BatchIteratorFactory $batchIteratorFactory
24+
) {
25+
$this->metadata = $metadata;
26+
$this->resourceConnection = $resourceConnection;
27+
$this->batchIteratorFactory = $batchIteratorFactory;
28+
}
29+
30+
public function countOrders(): int
31+
{
32+
$tableName = $this->resourceConnection->getTableName($this->metadata->getFeedTableName());
33+
$connection = $this->resourceConnection->getConnection();
34+
35+
$select = $connection->select()
36+
->from(
37+
['order' => $tableName],
38+
['count' => 'COUNT(*)']
39+
);
40+
$row = $connection->fetchRow($select);
41+
return $row['count'];
42+
}
43+
44+
public function fetchOrders(int $batchSize = 100): Generator
45+
{
46+
$tableName = $this->resourceConnection->getTableName($this->metadata->getFeedTableName());
47+
$connection = $this->resourceConnection->getConnection();
48+
49+
$select = $connection->select()
50+
->from(
51+
['order' => $tableName],
52+
['id', 'feed_data']
53+
);
54+
55+
$iterator = $this->batchIteratorFactory->create(
56+
[
57+
'select' => $select,
58+
'batchSize' => $batchSize,
59+
'correlationName' => 'order',
60+
'rangeField' => 'id',
61+
'rangeFieldAlias' => 'id',
62+
]
63+
);
64+
65+
foreach ($iterator as $batchSelect) {
66+
yield $connection->fetchAll($batchSelect);
67+
}
68+
}
69+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\SalesOrdersDataExporter\Model;
9+
10+
use DateTime;
11+
use DateTimeInterface;
12+
use Magento\DataExporter\Model\Indexer\FeedIndexMetadata;
13+
use Magento\DataExporter\Model\Logging\CommerceDataExportLoggerInterface;
14+
use Magento\Framework\Console\Cli;
15+
use Magento\SalesOrdersDataExporter\Console\Command\Link;
16+
use Magento\SalesOrdersDataExporter\Model\Indexer\DateTimeRangeOrderProcessor;
17+
use Symfony\Component\Console\Output\NullOutput;
18+
use Symfony\Component\Console\Output\OutputInterface;
19+
20+
class OnDemandOrdersExporter
21+
{
22+
private CommerceDataExportLoggerInterface $logger;
23+
private FeedIndexMetadata $metadata;
24+
private DateTimeRangeOrderProcessor $processor;
25+
private Link $linkCommand;
26+
27+
public function __construct(
28+
CommerceDataExportLoggerInterface $logger,
29+
FeedIndexMetadata $metadata,
30+
DateTimeRangeOrderProcessor $processor,
31+
Link $linkCommand
32+
) {
33+
$this->logger = $logger;
34+
$this->metadata = $metadata;
35+
$this->processor = $processor;
36+
$this->linkCommand = $linkCommand;
37+
}
38+
39+
public function export(DateTime $from, DateTime $to, OutputInterface $output = null): void
40+
{
41+
$output = $output ?? new NullOutput();
42+
$this->ensureAssignedUuids($from, $to, $output);
43+
$this->processor->fullReindex($this->metadata, $from, $to);
44+
}
45+
46+
private function ensureAssignedUuids(DateTime $from, DateTime $to, OutputInterface $output): void
47+
{
48+
$returnCode = $this->linkCommand->prepareForExport(
49+
$this->metadata->getBatchSize(),
50+
$output,
51+
$from->format(DateTimeInterface::W3C),
52+
$to->format(DateTimeInterface::W3C)
53+
);
54+
if ($returnCode != Cli::RETURN_SUCCESS) {
55+
$this->logger->error('Command "commerce-data-export:orders:link" failed.');
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)