Failed Conditions
Push — master ( dedbae...8af529 )
by Sébastien
02:19
created

ConvertDirCommand::execute()   B

Complexity

Conditions 6
Paths 36

Size

Total Lines 96
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 40
CRAP Score 6.0038

Importance

Changes 0
Metric Value
eloc 46
dl 0
loc 96
ccs 40
cts 42
cp 0.9524
rs 8.5559
c 0
b 0
f 0
cc 6
nc 36
nop 2
crap 6.0038

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
2
3
declare(strict_types=1);
4
5
/**
6
 * @see       https://github.com/soluble-io/soluble-mediatools-cli for the canonical repository
7
 *
8
 * @copyright Copyright (c) 2018-2019 Sébastien Vanvelthem. (https://github.com/belgattitude)
9
 * @license   https://github.com/soluble-io/soluble-mediatools-cli/blob/master/LICENSE.md MIT
10
 */
11
12
namespace Soluble\MediaTools\Cli\Command;
13
14
use Soluble\MediaTools\Cli\FileSystem\DirectoryScanner;
15
use Soluble\MediaTools\Cli\Media\FileExtensions;
16
use Soluble\MediaTools\Cli\Service\MediaToolsServiceInterface;
17
use Soluble\MediaTools\Common\Exception\ProcessException;
18
use Soluble\MediaTools\Preset\PresetInterface;
19
use Soluble\MediaTools\Preset\PresetLoader;
20
use Symfony\Component\Console\Command\Command;
21
use Symfony\Component\Console\Helper\ProgressBar;
22
use Symfony\Component\Console\Input\InputDefinition;
23
use Symfony\Component\Console\Input\InputInterface;
24
use Symfony\Component\Console\Input\InputOption;
25
use Symfony\Component\Console\Output\OutputInterface;
26
use Webmozart\Assert\Assert;
27
28
class ConvertDirCommand extends Command
29
{
30
    /** @var MediaToolsServiceInterface */
31
    private $mediaTools;
32
33
    /** @var PresetLoader */
34
    private $presetLoader;
35
36
    /** @var string[] */
37
    private $supportedVideoExtensions;
38
39 3
    public function __construct(MediaToolsServiceInterface $mediaTools, PresetLoader $presetLoader)
40
    {
41 3
        $this->mediaTools               = $mediaTools;
42 3
        $this->presetLoader             = $presetLoader;
43 3
        $this->supportedVideoExtensions = (new FileExtensions())->getMediaExtensions();
44 3
        parent::__construct();
45 3
    }
46
47 3
    protected function configure(): void
48
    {
49
        $this
50 3
            ->setName('convert:directory')
51 3
            ->setDescription('Convert all media files in a directory using a preset')
52 3
            ->setDefinition(
53 3
                new InputDefinition([
54 3
                    new InputOption('dir', ['d'], InputOption::VALUE_REQUIRED, 'Input directory to scan for medias'),
55 3
                    new InputOption('preset', ['p'], InputOption::VALUE_REQUIRED, 'Conversion preset to use'),
56 3
                    new InputOption('exts', ['e', 'extensions'], InputOption::VALUE_OPTIONAL, 'File extensions to process (ie. m4v,mp4,mov)'),
57 3
                    new InputOption('output', ['o', 'out'], InputOption::VALUE_REQUIRED, 'Output directory'),
58
                ])
59
            );
60 3
    }
61
62 3
    protected function execute(InputInterface $input, OutputInterface $output): int
63
    {
64
        // ########################
65
        // Step 1: Check directory
66
        // ########################
67
68 3
        $directory = $input->getOption('dir');
69 3
        Assert::stringNotEmpty($directory);
70 2
        Assert::directory($directory);
71
72
        // ########################
73
        // Step 2: Init preset
74
        // ########################
75
76 1
        Assert::stringNotEmpty($input->getOption('preset'));
77 1
        $preset = $this->getPreset($input->getOption('preset'));
78
79
        // ########################
80
        // Step 3: Output dir
81
        // ########################
82
83 1
        if ($input->getOption('output') !== null) {
84 1
            $outputDir = $input->getOption('output');
85 1
            Assert::directory($outputDir);
86 1
            Assert::writable($outputDir);
87
        } else {
88
            $outputDir = $directory;
89
        }
90 1
        Assert::stringNotEmpty($outputDir);
91
92
        // ########################
93
        // Step 4: Extensions
94
        // ########################
95
96 1
        if ($input->getOption('exts') !== null) {
97 1
            $tmp = $input->getOption('exts');
98 1
            Assert::stringNotEmpty($tmp);
99 1
            $exts = array_filter(
100 1
                array_map(
101 1
                    'trim',
102 1
                    explode(',', $tmp)
0 ignored issues
show
Bug introduced by
It seems like $tmp can also be of type string[]; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

102
                    explode(',', /** @scrutinizer ignore-type */ $tmp)
Loading history...
103
                )
104
            );
105 1
            Assert::minCount($exts, 1);
106
        } else {
107
            $exts = FileExtensions::BUILTIN_EXTENSIONS;
108
        }
109
110
        // ########################
111
        // Step 5: Scanning dir
112
        // ########################
113
114 1
        $output->writeln(sprintf('* Scanning %s for media files...', $directory));
0 ignored issues
show
Bug introduced by
It seems like $directory can also be of type string[]; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

114
        $output->writeln(sprintf('* Scanning %s for media files...', /** @scrutinizer ignore-type */ $directory));
Loading history...
115
116
        // Get the videos in path
117 1
        $files = (new DirectoryScanner())->findFiles($directory, $exts);
118
119 1
        $output->writeln('* Reading metadata...');
120
121 1
        $progressBar = new ProgressBar($output, count($files));
122
        //$progressBar->start();
123
124 1
        $converter = $this->mediaTools->getConverter();
125
126
        /** @var \SplFileInfo $file */
127 1
        foreach ($files as $file) {
128
            try {
129 1
                $params = $preset->getParams($file->getPathname());
130
131 1
                $outputFile = sprintf(
132 1
                    '%s/%s%s',
133
                    $outputDir,
134 1
                    $file->getBasename($file->getExtension()),
135 1
                    $preset->getFileExtension()
136
                );
137
138 1
                if (!file_exists($outputFile)) {
139
                    $converter->convert((string) $file, $outputFile, $params, function ($stdOut, $stdErr) use ($output): void {
140 1
                        $output->write($stdErr);
141 1
                    });
142
143 1
                    $output->writeln(sprintf('<fg=green>- Converted:</> %s.', $file));
144
                } else {
145 1
                    $output->writeln(sprintf('<fg=yellow>- Skipped:</> %s : Output file already exists.', $file));
146
                }
147 1
            } catch (ProcessException $e) {
148 1
                $output->writeln(sprintf('<fg=red>- Skipped:</> %s : Not a valid media file.', $file));
149
            }
150
151
            //$progressBar->advance();
152
        }
153
154 1
        $progressBar->finish();
155 1
        $output->writeln('');
156
157 1
        return 0;
158
    }
159
160 1
    private function getPreset(string $presetName): PresetInterface
161
    {
162 1
        return $this->presetLoader->getPreset($presetName);
163
    }
164
}
165