AbstractDatabaseCollector   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
c 0
b 0
f 0
dl 0
loc 108
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A createIteratorAndPrepareQuery() 0 12 1
A processBatchForExport() 0 16 1
A exportDataToStore() 0 25 2
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;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\BatchResultTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Generated\Shared\Transfer\LocaleTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\LocaleTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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\File\FileWriterInterface;
17
use SprykerEco\Zed\Econda\Persistence\Econda\AbstractEcondaPdoQuery;
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\AbstractEcondaPdoQuery
25
     */
26
    protected $econdaPdoQuery;
27
28
    /**
29
     * @var \Spryker\Shared\SqlCriteriaBuilder\CriteriaBuilder\CriteriaBuilderInterface
30
     */
31
    protected $criteriaBuilder;
32
33
    /**
34
     * @param \Spryker\Shared\SqlCriteriaBuilder\CriteriaBuilder\CriteriaBuilderInterface $criteriaBuilder
35
     * @param \SprykerEco\Zed\Econda\Persistence\Econda\AbstractEcondaPdoQuery $econdaPdoQuery
36
     */
37
    public function __construct(
38
        CriteriaBuilderInterface $criteriaBuilder,
39
        AbstractEcondaPdoQuery $econdaPdoQuery
40
    ) {
41
        $this->criteriaBuilder = $criteriaBuilder;
42
        $this->econdaPdoQuery = $econdaPdoQuery;
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
    ): CountableIteratorInterface {
59
        $this->econdaPdoQuery
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\File\FileWriterInterface $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
        FileWriterInterface $storeWriter,
81
        LocaleTransfer $locale,
82
        OutputInterface $output,
83
        ProgressBar $progressBar
84
    ): void {
85
        $totalCount = $batchCollection->count();
86
        $batchResult->setTotalCount($totalCount);
87
88
        $progressBar->setMessage($this->collectResourceType(), 'barTitle');
89
90
        foreach ($batchCollection as $batch) {
91
            $this->processBatchForExport(
92
                $batch,
93
                $progressBar,
94
                $locale,
95
                $batchResult,
96
                $storeWriter
97
            );
98
        }
99
100
        $progressBar->finish();
101
        $output->writeln('');
102
    }
103
104
    /**
105
     * @param array $batch
106
     * @param \Symfony\Component\Console\Helper\ProgressBar $progressBar
107
     * @param \Generated\Shared\Transfer\LocaleTransfer $locale
108
     * @param \Generated\Shared\Transfer\BatchResultTransfer $batchResult
109
     * @param \SprykerEco\Zed\Econda\Business\Exporter\Writer\File\FileWriterInterface $storeWriter
110
     *
111
     * @return void
112
     */
113
    protected function processBatchForExport(
114
        array $batch,
115
        ProgressBar $progressBar,
116
        LocaleTransfer $locale,
117
        BatchResultTransfer $batchResult,
118
        FileWriterInterface $storeWriter
119
    ): void {
120
        $batchSize = count($batch);
121
        $progressBar->advance($batchSize);
122
123
        $collectedData = $this->collectData($batch, $locale);
124
        $collectedDataCount = count($collectedData);
125
126
        $storeWriter->write($collectedData);
127
128
        $batchResult->setProcessedCount($batchResult->getProcessedCount() + $collectedDataCount);
129
    }
130
}
131