EcondaFileExportConsole::execute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 9
rs 10
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\Communication\Console;
9
10
use Spryker\Zed\Kernel\Communication\Console\Console;
11
use Symfony\Component\Console\Helper\HelperSet;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
/**
16
 * @method \SprykerEco\Zed\Econda\Business\EcondaFacadeInterface getFacade()
17
 * @method \SprykerEco\Zed\Econda\Persistence\EcondaQueryContainerInterface getQueryContainer()
18
 */
19
class EcondaFileExportConsole extends Console
20
{
21
    protected const COMMAND_NAME = 'econda:file:export';
22
23
    protected const COMMAND_DESCRIPTION = 'Export data to files';
24
25
    /**
26
     * @return \Symfony\Component\Console\Helper\HelperSet
27
     */
28
    public function getHelperSet(): HelperSet
29
    {
30
        return new HelperSet();
31
    }
32
33
    /**
34
     * @return void
35
     */
36
    protected function configure(): void
37
    {
38
        $this->setName(static::COMMAND_NAME);
39
        $this->setDescription(static::COMMAND_DESCRIPTION);
40
41
        parent::configure();
42
    }
43
44
    /**
45
     * @param \Symfony\Component\Console\Input\InputInterface $input
46
     * @param \Symfony\Component\Console\Output\OutputInterface $output
47
     *
48
     * @return int
49
     */
50
    protected function execute(InputInterface $input, OutputInterface $output): int
51
    {
52
        $exportResults = $this->getFacade()->exportFile($output);
53
        $message = $this->buildNestedSummary($exportResults);
54
        $message = '<info>' . $message . '</info>';
55
56
        $output->write($message);
57
58
        return static::CODE_SUCCESS;
59
    }
60
61
    /**
62
     * @param array $resultData
63
     *
64
     * @return string
65
     */
66
    protected function buildSummary(array $resultData): string
67
    {
68
        if (empty($resultData)) {
69
            return PHP_EOL . '<fg=yellow>Nothing exported.</fg=yellow>' . PHP_EOL;
70
        }
71
72
        $summary = PHP_EOL;
73
74
        ksort($resultData);
75
        foreach ($resultData as $type => $result) {
76
            $successCount = $result->getTotalCount() - $result->getFailedCount();
77
            $summary .= sprintf(
78
                ' <fg=green>%s</fg=green><fg=yellow> </fg=yellow><fg=yellow></fg=yellow>' . PHP_EOL .
79
                ' <fg=white>Total: %d</fg=white>' . PHP_EOL .
80
                ' <fg=white>Processed: %d</fg=white>' . PHP_EOL .
81
                ' <fg=white>Succeeded: %s</fg=white>' . PHP_EOL .
82
                ' <fg=white>Deleted: %s</fg=white>' . PHP_EOL .
83
                ' <fg=white>Failed: %s </fg=white>' . PHP_EOL,
84
                mb_strtoupper($type),
85
                $result->getTotalCount(),
86
                $result->getProcessedCount(),
87
                $successCount > 0 ? '<fg=green>' . $successCount . '</fg=green>' : $successCount,
88
                $result->getDeletedCount() > 0 ? '<fg=yellow>' . $result->getDeletedCount() . '</fg=yellow>' : $result->getDeletedCount(),
89
                $result->getFailedCount() ? '<fg=red>' . $result->getFailedCount() . '</fg=red>' : $result->getFailedCount()
90
            );
91
92
            $summary .= PHP_EOL;
93
        }
94
95
        return $summary . PHP_EOL;
96
    }
97
98
    /**
99
     * @param array $results
100
     *
101
     * @return string
102
     */
103
    protected function buildNestedSummary(array $results): string
104
    {
105
        $summary = '';
106
        foreach ($results as $localeName => $summaryData) {
107
            $summary .= PHP_EOL;
108
            $summary .= '<fg=yellow>----------------------------------------</fg=yellow>';
109
            $summary .= PHP_EOL;
110
            $summary .= sprintf('<fg=yellow>Summary:</fg=yellow> <fg=white>%s</fg=white>', $localeName);
111
            $summary .= PHP_EOL;
112
            $summary .= $this->buildSummary($summaryData);
113
        }
114
115
        $summary .= PHP_EOL . 'All done.' . PHP_EOL;
116
117
        return $summary;
118
    }
119
}
120