Completed
Push — master ( f62a62...1e6a21 )
by Ma
09:49
created

DumpFileCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 2
cbo 5
dl 0
loc 86
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 22 1
B execute() 0 46 4
1
<?php
2
3
/**
4
 * Aist Filesystem Component (http://mateuszsitek.com/projects/filesystem)
5
 *
6
 * @copyright Copyright (c) 2017 DIGITAL WOLVES LTD (http://digitalwolves.ltd) All rights reserved.
7
 * @license   http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
8
 */
9
10
namespace Aist\Filesystem\Console\Command;
11
12
use Symfony\Component\Console\Command\Command;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Output\OutputInterface;
16
use Symfony\Component\Console\Style\SymfonyStyle;
17
18
/**
19
 * Dump file command
20
 */
21
class DumpFileCommand extends Command
22
{
23
    const NAME = 'filesystem:dump-file';
24
25
    const DESCRIPTION = 'Filesystem dump file.';
26
27
    const HELP = <<< 'EOT'
28
Filesystem dump file.
29
EOT;
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    protected function configure()
35
    {
36
        $this
37
            ->setName(self::NAME)
38
            ->setDescription(self::DESCRIPTION)
39
            ->setHelp(self::HELP)
40
            ->setDefinition([
41
                new InputOption(
42
                    'target',
43
                    't',
44
                    InputOption::VALUE_REQUIRED,
45
                    'Target path.'
46
                ),
47
                new InputOption(
48
                    'content',
49
                    'c',
50
                    InputOption::VALUE_REQUIRED,
51
                    'Content.'
52
                ),
53
            ])
54
        ;
55
    }
56
57
    /**
58
     * Executes the command
59
     */
60
    protected function execute(InputInterface $input, OutputInterface $output)
61
    {
62
        /**
63
         * Strange issue
64
         * Without this we can't use $this->getHelper('name')
65
         * but $this->getApplication()->getHelperSet()->get('name')
66
         */
67
        $this->setApplication($this->getApplication());
68
        $filesystem = $this->getHelper('filesystem');
69
        $logger = $this->getHelper('logger');
70
        $io = new SymfonyStyle($input, $output);
71
72
        $target = $input->getOption('target');
73
        $content = $input->getOption('content');
74
75
        try {
76
            $filesystem->dumpFile($target, $content);
0 ignored issues
show
Bug introduced by
The method dumpFile() does not seem to exist on object<Symfony\Component...Helper\HelperInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
77
            if (! $output->isQuiet()) {
78
                $output->writeln(sprintf('  - Saved <info>%s</info>', $target));
79
            }
80
            $logger->info(
0 ignored issues
show
Bug introduced by
The method info() does not seem to exist on object<Symfony\Component...Helper\HelperInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
81
                self::class,
82
                [
83
                    'target' => $target,
84
                    'content' => $content,
85
                ]
86
            );
87
        } catch (\Exception $exception) {
88
            if (! $output->isQuiet()) {
89
                $io->error($exception->getMessage());
90
            }
91
            $logger->error(
0 ignored issues
show
Bug introduced by
The method error() does not seem to exist on object<Symfony\Component...Helper\HelperInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
92
                self::class,
93
                [
94
                    'code' => $exception->getCode(),
95
                    'message' => $exception->getMessage(),
96
                    'target' => $target,
97
                    'content' => $content,
98
                ]
99
            );
100
101
            return 1;
102
        }
103
104
        return 0;
105
    }
106
}
107