Completed
Push — development ( f6174b...9d8e04 )
by Theodoros
09:35
created

Runner::exportStoreByLocale()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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