Passed
Branch development (62ef01)
by Theodoros
06:18
created

AbstractDatabaseCollector::exportDataToStore()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 6
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * MIT License
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Zed\Econda\Business\Collector;
9
10
use Generated\Shared\Transfer\BatchResultTransfer;
11
use Generated\Shared\Transfer\LocaleTransfer;
12
use Spryker\Service\UtilDataReader\Model\BatchIterator\CountableIteratorInterface;
13
use Spryker\Service\UtilDataReader\Model\BatchIterator\PdoBatchIterator;
14
use Spryker\Shared\SqlCriteriaBuilder\CriteriaBuilder\CriteriaBuilderInterface;
15
use Spryker\Zed\Kernel\Persistence\QueryContainer\QueryContainerInterface;
16
use SprykerEco\Zed\Econda\Business\Exporter\Writer\WriterInterface;
17
use SprykerEco\Zed\Econda\Persistence\Econda\AbstractPdoEcondaQuery;
18
use Symfony\Component\Console\Helper\ProgressBar;
19
use Symfony\Component\Console\Output\OutputInterface;
20
21
abstract class AbstractDatabaseCollector extends AbstractCollector implements DatabaseCollectorInterface
22
{
23
    /**
24
     * @var \SprykerEco\Zed\Econda\Persistence\Econda\AbstractPdoEcondaQuery
25
     */
26
    protected $pdoEcondaQuery;
27
28
    /**
29
     * @var \Spryker\Shared\SqlCriteriaBuilder\CriteriaBuilder\CriteriaBuilderInterface
30
     */
31
    protected $criteriaBuilder;
32
33
    /**
34
     * @param \Spryker\Shared\SqlCriteriaBuilder\CriteriaBuilder\CriteriaBuilderInterface $criteria
35
     * @param \SprykerEco\Zed\Econda\Persistence\Econda\AbstractPdoEcondaQuery $pdoEcondaQuery
36
     */
37
    public function __construct(
38
        CriteriaBuilderInterface $criteria,
39
        AbstractPdoEcondaQuery $pdoEcondaQuery
40
    ) {
41
        $this->criteriaBuilder = $criteria;
42
        $this->pdoEcondaQuery = $pdoEcondaQuery;
43
    }
44
45
    /**
46
     * @param \Generated\Shared\Transfer\LocaleTransfer $locale
47
     * @param \Spryker\Shared\SqlCriteriaBuilder\CriteriaBuilder\CriteriaBuilderInterface $criteriaBuilder
48
     * @param \Spryker\Zed\Kernel\Persistence\QueryContainer\QueryContainerInterface $queryContainer
49
     * @param int $chunkSize
50
     *
51
     * @return \Spryker\Service\UtilDataReader\Model\BatchIterator\CountableIteratorInterface
52
     */
53
    public function createIteratorAndPrepareQuery(
54
        LocaleTransfer $locale,
55
        CriteriaBuilderInterface $criteriaBuilder,
56
        QueryContainerInterface $queryContainer,
57
        $chunkSize = 100
58
    ) {
59
        $this->pdoEcondaQuery
60
            ->setCriteriaBuilder($criteriaBuilder)
61
            ->setLocale($locale)
62
            ->prepare();
63
64
        return new PdoBatchIterator($criteriaBuilder, $queryContainer, $chunkSize);
65
    }
66
67
    /**
68
     * @param \Spryker\Service\UtilDataReader\Model\BatchIterator\CountableIteratorInterface $batchCollection
69
     * @param \Generated\Shared\Transfer\BatchResultTransfer $batchResult
70
     * @param \SprykerEco\Zed\Econda\Business\Exporter\Writer\WriterInterface $storeWriter
71
     * @param \Generated\Shared\Transfer\LocaleTransfer $locale
72
     * @param \Symfony\Component\Console\Output\OutputInterface $output
73
     * @param \Symfony\Component\Console\Helper\ProgressBar $progressBar
74
     *
75
     * @return void
76
     */
77
    public function exportDataToStore(
78
        CountableIteratorInterface $batchCollection,
79
        BatchResultTransfer $batchResult,
80
        WriterInterface $storeWriter,
81
        LocaleTransfer $locale,
82
        OutputInterface $output,
83
        ProgressBar $progressBar
84
    ) {
85
86
        $totalCount = $batchCollection->count();
87
        $batchResult->setTotalCount($totalCount);
88
89
        $progressBar->setMessage($this->collectResourceType(), 'barTitle');
90
91
        foreach ($batchCollection as $batch) {
92
            $this->processBatchForExport(
93
                $batch,
94
                $progressBar,
95
                $locale,
96
                $batchResult,
97
                $storeWriter
98
            );
99
        }
100
101
        $progressBar->finish();
102
        $output->writeln('');
103
    }
104
105
    /**
106
     * @param array $batch
107
     * @param \Symfony\Component\Console\Helper\ProgressBar $progressBar
108
     * @param \Generated\Shared\Transfer\LocaleTransfer $locale
109
     * @param \Generated\Shared\Transfer\BatchResultTransfer $batchResult
110
     * @param \SprykerEco\Zed\Econda\Business\Exporter\Writer\WriterInterface $storeWriter
111
     *
112
     * @return void
113
     */
114
    protected function processBatchForExport(
115
        array $batch,
116
        ProgressBar $progressBar,
117
        LocaleTransfer $locale,
118
        BatchResultTransfer $batchResult,
119
        WriterInterface $storeWriter
120
    ) {
121
        $batchSize = count($batch);
122
        $progressBar->advance($batchSize);
123
124
        $collectedData = $this->collectData($batch, $locale);
125
        $collectedDataCount = count($collectedData);
126
127
        $storeWriter->write($collectedData);
128
129
        $batchResult->setProcessedCount($batchResult->getProcessedCount() + $collectedDataCount);
130
    }
131
}
132