Issues (2)

src/Command/Import.php (1 issue)

1
<?php
2
declare(strict_types=1);
3
4
namespace Rarst\Hugo\wprss2hugo\Command;
5
6
use Pimple\Container;
7
use Rarst\Hugo\wprss2hugo\Export;
8
use Rarst\Hugo\wprss2hugo\Processor;
9
use Rarst\Hugo\wprss2hugo\Serializer\Html;
10
use Rarst\Hugo\wprss2hugo\Serializer\Json;
11
use Rarst\Hugo\wprss2hugo\Serializer\Markdown;
12
use Rarst\Hugo\wprss2hugo\Serializer\Toml;
13
use Rarst\Hugo\wprss2hugo\Serializer\Yaml;
14
use Symfony\Component\Console\Helper\ProgressBar;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
/**
18
 * Command callable for import.
19
 */
20
class Import
21
{
22
    /** @var Container */
23
    private $container;
24
25
    /**
26
     * Set up with dependency injection container instance.
27
     */
28
    public function __construct(Container $container)
29
    {
30
        $this->container = $container;
31
    }
32
33
    /**
34
     * Command callback with command line arguments.
35
     */
36
    public function __invoke(
37
        string $file,
38
        OutputInterface $output,
39
        string $contentType = 'html',
40
        string $frontMatterType = 'yaml',
41
        string $dataType = 'yaml'
42
    ) {
43
        $export      = new Export($file);
44
        $processor   = $this->processor($contentType, $frontMatterType, $dataType);
45
        $progressBar = new ProgressBar($output, 100);
46
        foreach ($export as $node) {
47
            $processor->push($node);
48
            $progressBar->setProgress($export->progress());
49
        }
50
51
        $progressBar->finish();
52
        $output->write(PHP_EOL . PHP_EOL);
53
        $processor->store();
54
55
        foreach ($processor->getCounts() as $type => $count) {
56
            $output->writeln("Processed '{$type}': {$count}");
57
        }
58
    }
59
60
    /**
61
     * Pass command line arguments to the container and retrieve configured Processor instance.
62
     */
63
    private function processor(string $content, string $frontMatter, string $data): Processor
64
    {
65
        $this->container['serializer.content.class']      = $this->serializerClass($content);
66
        $this->container['serializer.front-matter.class'] = $this->serializerClass($frontMatter);
67
        $this->container['serializer.data.class']         = $this->serializerClass($data);
68
69
        return $this->container['processor'];
70
    }
71
72
    /**
73
     * Retrieve serializer class name for a given file type.
74
     */
75
    private function serializerClass(string $type): string
76
    {
77
        switch ($type) {
78
            case 'yaml':
79
                return Yaml::class;
80
                break;
0 ignored issues
show
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
81
            case 'toml':
82
                return Toml::class;
83
                break;
84
            case 'json':
85
                return Json::class;
86
                break;
87
            case 'html':
88
                return Html::class;
89
                break;
90
            case 'md':
91
                return Markdown::class;
92
                break;
93
        }
94
95
        throw new \UnexpectedValueException("Unknown type '{$type}'.");
96
    }
97
}
98