Failed Conditions
Push — master ( 346462...6993a9 )
by Sébastien
02:17
created

ConvertDirCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 1
rs 10
c 0
b 0
f 0
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\Video\VideoAnalyzerInterface;
16
use Soluble\MediaTools\Video\VideoConverterInterface;
17
use Soluble\MediaTools\Video\VideoInfoReaderInterface;
18
use Symfony\Component\Console\Command\Command;
19
use Symfony\Component\Console\Helper\ProgressBar;
20
use Symfony\Component\Console\Input\InputDefinition;
21
use Symfony\Component\Console\Input\InputInterface;
22
use Symfony\Component\Console\Input\InputOption;
23
use Symfony\Component\Console\Output\OutputInterface;
24
25
class ConvertDirCommand extends Command
26
{
27
    /** @var VideoInfoReaderInterface */
28
    protected $videoInfoReader;
29
30
    /** @var VideoAnalyzerInterface */
31
    protected $videoAnalyzer;
32
33
    /** @var VideoConverterInterface */
34
    protected $videoConverter;
35
36
    /** @var string[] */
37
    protected $supportedVideoExtensions = [
38
        'mov',
39
        'mp4',
40
        'mkv',
41
        'flv',
42
        'webm',
43
    ];
44
45 3
    public function __construct(VideoInfoReaderInterface $videoInfoReader, VideoAnalyzerInterface $videoAnalyzer, VideoConverterInterface $videoConverter)
46
    {
47 3
        $this->videoInfoReader = $videoInfoReader;
48 3
        $this->videoAnalyzer   = $videoAnalyzer;
49 3
        $this->videoConverter  = $videoConverter;
50 3
        parent::__construct();
51 3
    }
52
53
    /**
54
     * Configures the command.
55
     */
56 3
    protected function configure(): void
57
    {
58
        $this
59 3
            ->setName('convert:directory')
60 3
            ->setDescription('Convert, transcode all media files in a directory')
61 3
            ->setDefinition(
62 3
                new InputDefinition([
63 3
                    new InputOption('dir', 'd', InputOption::VALUE_REQUIRED),
64
                ])
65
            );
66 3
    }
67
68 3
    protected function execute(InputInterface $input, OutputInterface $output): int
69
    {
70 3
        if (!$input->hasOption('dir')) {
71
            throw new \InvalidArgumentException('Missing dir argument, use <command> <dir>');
72
        }
73 3
        $directory = $input->hasOption('dir') ? $input->getOption('dir') : null;
74 3
        if (!is_string($directory) || !is_dir($directory)) {
75 2
            throw new \InvalidArgumentException(sprintf(
76 2
                'Directory %s does not exists',
77 2
                is_string($directory) ? $directory : json_encode($directory)
78
            ));
79
        }
80
81 1
        $output->writeln(sprintf('* Scanning %s for media files...', $directory));
82
83
        // Get the videos in path
84 1
        $files = (new DirectoryScanner())->findFiles($directory, $this->supportedVideoExtensions);
85
86 1
        $output->writeln('* Reading metadata...');
87
88 1
        $progressBar = new ProgressBar($output, count($files));
89 1
        $progressBar->start();
90
91
        /* @var \SplFileInfo $video */
92 1
        foreach ($files as $file) {
93 1
            $progressBar->advance();
94
        }
95
96 1
        $progressBar->finish();
97 1
        $output->writeln('');
98
99 1
        return 0;
100
    }
101
}
102