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

Runner   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 124
rs 10
c 0
b 0
f 0
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getEnabledExports() 0 3 1
A nothingWasProcessed() 0 3 1
A handleResult() 0 11 2
A runExport() 0 23 3
B exportStoreByLocale() 0 23 4
A __construct() 0 6 1
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\Exporter;
9
10
use Generated\Shared\Transfer\BatchResultTransfer;
11
use Generated\Shared\Transfer\LocaleTransfer;
12
use Spryker\Shared\Kernel\Store;
13
use SprykerEco\Zed\Econda\Business\Exporter\Exception\BatchResultException;
14
use SprykerEco\Zed\Econda\Dependency\Facade\EcondaToLocaleInterface;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
class Runner
18
{
19
    /**
20
     * @var \SprykerEco\Zed\Econda\Business\Exporter\ExporterInterface
21
     */
22
    protected $exporter;
23
24
    /**
25
     * @var \SprykerEco\Zed\Econda\Dependency\Facade\EcondaToLocaleInterface
26
     */
27
    protected $localeFacade;
28
29
    /**
30
     * @param \SprykerEco\Zed\Econda\Dependency\Facade\EcondaToLocaleInterface $localeFacade
31
     * @param \SprykerEco\Zed\Econda\Business\Exporter\ExporterInterface $exporter
32
     */
33
    public function __construct(
34
        EcondaToLocaleInterface $localeFacade,
35
        ExporterInterface $exporter
36
    ) {
37
        $this->localeFacade = $localeFacade;
38
        $this->exporter = $exporter;
39
    }
40
41
    /**
42
     * @param \Symfony\Component\Console\Output\OutputInterface $output
43
     *
44
     * @return array
45
     */
46
    public function runExport(OutputInterface $output)
47
    {
48
        $storeCollection = Store::getInstance()->getAllowedStores();
49
50
        $results = [];
51
52
        foreach ($storeCollection as $storeName) {
53
            $output->writeln('');
54
            $output->writeln('<fg=yellow>----------------------------------------</fg=yellow>');
55
            $output->writeln(sprintf(
56
                '<fg=yellow>Exporting Store:</fg=yellow> <fg=white>%s</fg=white>',
57
                $storeName
58
            ));
59
            $output->writeln('');
60
61
            $localeCollection = Store::getInstance()->getLocalesPerStore($storeName);
62
            foreach ($localeCollection as $locale => $localeCode) {
63
                $localeTransfer = $this->localeFacade->getLocale($localeCode);
64
                $results[$storeName . '@' . $localeCode] = $this->exportStoreByLocale($localeTransfer, $output);
65
            }
66
        }
67
68
        return $results;
69
    }
70
71
    /**
72
     * @param \Generated\Shared\Transfer\LocaleTransfer $locale
73
     * @param \Symfony\Component\Console\Output\OutputInterface $output
74
     *
75
     * @return array
76
     */
77
    protected function exportStoreByLocale(LocaleTransfer $locale, OutputInterface $output)
78
    {
79
        $results = [];
80
        $types = $this->getEnabledExports();
81
82
        $output->writeln('');
83
        $output->writeln(sprintf('<fg=yellow>Locale:</fg=yellow> <fg=white>%s</fg=white>', $locale->getLocaleName()));
84
        $output->writeln('<fg=yellow>-------------</fg=yellow>');
85
86
        foreach ($types as $type) {
87
            $result = $this->exporter->exportByType($type, $locale, $output);
88
89
            $this->handleResult($result);
90
91
            if ($result instanceof BatchResultTransfer) {
92
                if ($this->nothingWasProcessed($result)) {
93
                    continue;
94
                }
95
                $results[$type] = $result;
96
            }
97
        }
98
99
        return $results;
100
    }
101
102
    /**
103
     * @param \Generated\Shared\Transfer\BatchResultTransfer $result
104
     *
105
     * @return bool
106
     */
107
    protected function nothingWasProcessed(BatchResultTransfer $result)
108
    {
109
        return $result->getProcessedCount() === 0;
110
    }
111
112
    /**
113
     * @param \Generated\Shared\Transfer\BatchResultTransfer $result
114
     *
115
     * @throws \SprykerEco\Zed\Econda\Business\Exporter\Exception\BatchResultException
116
     *
117
     * @return void
118
     */
119
    protected function handleResult(BatchResultTransfer $result)
120
    {
121
        if ($result->getFailedCount()) {
122
            throw new BatchResultException(
123
                sprintf(
124
                    'Processed %d from %d for locale %s, where %d were deleted and %d failed.',
125
                    $result->getProcessedCount(),
126
                    $result->getTotalCount(),
127
                    $result->getProcessedLocale()->getLocaleName(),
128
                    $result->getDeletedCount(),
129
                    $result->getFailedCount()
130
                )
131
            );
132
        }
133
    }
134
135
    /**
136
     * @return array
137
     */
138
    public function getEnabledExports()
139
    {
140
        return array_keys($this->exporter->getCollectorPlugins());
141
    }
142
}
143