Passed
Push — master ( 170dc8...c9950a )
by mon
02:34
created

UpdateCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 0
cts 19
cp 0
rs 9.6333
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace FileEye\MimeMap\Command;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use FileEye\MimeMap\MapUpdater;
10
use FileEye\MimeMap\MapHandler;
11
12
/**
13
 * A Symfony application command to update the MIME type to extension map.
14
 */
15
class UpdateCommand extends Command
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20
    protected function configure()
21
    {
22
        $this
23
            ->setName('update')
24
            ->setDescription('Updates the MIME-type-to-extension map.')
25
            ->addArgument(
26
                'source-url',
27
                InputArgument::OPTIONAL,
28
                'URL of the source map',
29
                MapUpdater::DEFAULT_URL
30
            )
31
            ->addArgument(
32
                'output-file',
33
                InputArgument::OPTIONAL,
34
                'Path to the directory of the mapper class PHP file',
35
                MapUpdater::getDefaultCodeFilePath()
36
            )
37
        ;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    protected function execute(InputInterface $input, OutputInterface $output)
44
    {
45
        $updater = new MapUpdater();
46
        try {
47
            $new_map_array = $updater->loadMapFromUrl($input->getArgument('source-url'));
48
            $new_map = new MapHandler($new_map_array);
49
        } catch (\RuntimeException $e) {
50
            $output->writeln('<error>' . $e->getMessage() . '</error>');
51
            exit(2);
52
        }
53
54
        // xx
55
        $new_map->setExtensionDefaultType('sub', 'text/vnd.dvb.subtitle');
56
        $new_map->setExtensionDefaultType('wmz', 'application/x-msmetafile');
57
        $new_map->sort();
58
59
        $current_map = new MapHandler();
60
        $write = false;
61
        try {
62
            $updater->compareMaps($current_map->get(), $new_map->get(), 'types');
63
        } catch (\RuntimeException $e) {
64
            $output->writeln('Changes to MIME types mapping:');
65
            $output->writeln($e->getMessage());
66
            $write = true;
67
        }
68
        try {
69
            $updater->compareMaps($current_map->get(), $new_map->get(), 'extensions');
70
        } catch (\RuntimeException $e) {
71
            $output->writeln('Changes to extensions mapping:');
72
            $output->writeln($e->getMessage());
73
            $write = true;
74
        }
75
        if ($write) {
76
            $updater->writeMapToCodeFile($new_map->get(), $input->getArgument('output-file'));
77
            $output->writeln('<comment>Code updated.</comment>');
78
        } else {
79
            $output->writeln('<info>No changes to mapping.</info>');
80
        }
81
    }
82
}
83