Passed
Branch development (0519a2)
by Theodoros
02:02
created

Runner::runExport()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 3
nop 1
dl 0
loc 23
rs 9.0856
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\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...
11
use Spryker\Shared\Kernel\Store;
0 ignored issues
show
Bug introduced by
The type Spryker\Shared\Kernel\Store 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 SprykerEco\Zed\Econda\Business\Exporter\Exception\BatchResultException;
13
use SprykerEco\Zed\Econda\Business\Model\BatchResultInterface;
14
use SprykerEco\Zed\Econda\Dependency\Facade\EcondaToLocaleInterface;
15
use Symfony\Component\Console\Output\OutputInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Output\OutputInterface 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...
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
89
            $result = $this->exporter->exportByType($type, $locale, $output);
90
91
            $this->handleResult($result);
92
93
            if ($result instanceof BatchResultInterface) {
94
                if ($this->nothingWasProcessed($result)) {
95
                    continue;
96
                }
97
                $results[$type] = $result;
98
            }
99
        }
100
101
        return $results;
102
    }
103
104
    /**
105
     * @param \SprykerEco\Zed\Econda\Business\Model\BatchResultInterface $result
106
     *
107
     * @return bool
108
     */
109
    protected function nothingWasProcessed(BatchResultInterface $result)
110
    {
111
        return $result->getProcessedCount() === 0;
112
    }
113
114
    /**
115
     * @param \SprykerEco\Zed\Econda\Business\Model\BatchResultInterface $result
116
     *
117
     * @throws \SprykerEco\Zed\Econda\Business\Exporter\Exception\BatchResultException
118
     *
119
     * @return void
120
     */
121
    protected function handleResult(BatchResultInterface $result)
122
    {
123
        if ($result->isFailed()) {
124
            throw new BatchResultException(
125
                sprintf(
126
                    'Processed %d from %d for locale %s, where %d were deleted and %d failed.',
127
                    $result->getProcessedCount(),
128
                    $result->getTotalCount(),
129
                    $result->getProcessedLocale(),
130
                    $result->getDeletedCount(),
131
                    $result->getFailedCount()
132
                )
133
            );
134
        }
135
    }
136
137
    /**
138
     * @return array
139
     */
140
    public function getEnabledExports()
141
    {
142
        return array_keys($this->exporter->getCollectorPlugins());
143
    }
144
145
}
146