Passed
Branch master (bbfb46)
by Jan
22:41 queued 14:44
created

DocsPlatformUpdates::execute()   B

Complexity

Conditions 8
Paths 25

Size

Total Lines 72
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 40
nc 25
nop 2
dl 0
loc 72
rs 8.0355
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
3
namespace Shopware\Docs\Command;
4
5
use Shopware\Docs\Convert\Document;
6
use Shopware\Docs\Convert\DocumentTree;
7
use Shopware\Docs\Convert\PlatformUpdatesDocument;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Console\Style\SymfonyStyle;
12
use Symfony\Component\Finder\Finder;
13
use Symfony\Component\Finder\SplFileInfo;
14
15
class DocsPlatformUpdates extends Command
16
{
17
    const TEMPLATE_HEADER = <<<EOD
18
[titleEn]: <>(Recent updates)
19
[__RAW__]: <>(__RAW__)
20
21
<p>Here you can find recent information about technical updates and news regarding <a href="https://github.com/shopware/platform">shopware platform</a>.</p>
22
23
<p><strong>New: Our public admin component library for easy scaffolding of your admin modules</strong></p>
24
25
<p><a href="https://component-library.shopware.com/">https://component-library.shopware.com</a></p>
26
27
EOD;
28
29
    const TEMPLATE_MONTH = <<<EOD
30
<h2>%s</h2>
31
32
EOD;
33
    const TEMPLATE_HEADLINE = <<<EOD
34
<h3>%s: %s</h3>
35
36
EOD;
37
38
    /**
39
     * @var string
40
     */
41
    private $platformUpdatesPath = __DIR__ . '/../Resources/platform-updates';
42
43
    /**
44
     * @var string
45
     */
46
    private $targetFile = __DIR__ . '/../_new/1-getting-started/40-recent-updates/__categoryInfo.md';
47
48
    public function __construct()
49
    {
50
        parent::__construct();
51
    }
52
53
    protected function configure(): void
54
    {
55
        $this
56
            ->setName('docs:dump-platform-updates')
57
            ->setDescription('Dumps all platform updates into a single file for the sync.');
58
    }
59
60
    protected function execute(InputInterface $input, OutputInterface $output)
61
    {
62
        $io = new SymfonyStyle($input, $output);
63
64
        $files = (new Finder())
65
            ->in($this->platformUpdatesPath)
66
            ->files()
67
            ->sortByName()
68
            ->depth('0')
69
            ->name('*.md')
70
            ->exclude('_archive.md')
71
            ->getIterator();
72
73
        $filesInOrder = array_reverse(iterator_to_array($files));
74
75
        $filesSorted = [];
76
77
        /** @var SplFileInfo $file */
78
        foreach ($filesInOrder as $file) {
79
            $baseName = $file->getBasename('.md');
80
81
            if ($baseName === '_archive') {
82
                continue;
83
            }
84
85
            $io->write("* Rendering: $baseName \n");
86
87
            $parts = explode('-', $baseName);
88
89
            if (count($parts) < 3) {
90
                throw new \Exception(sprintf('File %s is invalidly named', $file->getRelativePathname()));
91
            }
92
93
            $date = \DateTimeImmutable::createFromFormat('Y-m-d', implode('-', array_slice($parts, 0, 3)));
94
95
            $month = $date->format('Y-m');
96
97
            if (!isset($filesSorted[$month])) {
98
                $filesSorted[$month] = [];
99
            }
100
101
            $filesSorted[$month][] = new PlatformUpdatesDocument($date, $file, false, '');
102
        }
103
104
        $rendered = [self::TEMPLATE_HEADER];
105
        foreach ($filesSorted as $month => $documents) {
106
            $rendered[] = sprintf(self::TEMPLATE_MONTH,
107
                \DateTimeImmutable::createFromFormat('Y-m', $month)->format('F Y')
108
            );
109
110
            /** @var PlatformUpdatesDocument $document */
111
            foreach ($documents as $document) {
112
                $rendered[] = sprintf(self::TEMPLATE_HEADLINE,
113
                    $document->getDate()->format('Y-m-d'),
114
                    $document->getMetadata()->getTitleEn()
115
                );
116
117
                $rendered[] = $document->getHtml()->render(new DocumentTree())->getContents();
118
            }
119
        }
120
121
        $archivedfile = $this->platformUpdatesPath . '/_archive.md';
122
123
        if (file_exists($archivedfile)) {
124
            $io->note('Loading archive file');
125
            $rendered[] = file_get_contents($archivedfile);
126
        }
127
128
        $fileContents = implode(PHP_EOL, $rendered);
129
        file_put_contents($this->targetFile, $fileContents);
130
131
        $io->success('Done');
132
    }
133
}
134