CopyCommand::execute()   B
last analyzed

Complexity

Conditions 4
Paths 12

Size

Total Lines 48
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 48
rs 8.7396
cc 4
eloc 28
nc 12
nop 2
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
17
/**
18
 * Copy command
19
 */
20
class CopyCommand extends Command
21
{
22
    const NAME = 'filesystem:copy';
23
24
    const DESCRIPTION = 'Filesystem cp.';
25
26
    const HELP = <<< 'EOT'
27
Filesystem cp.
28
EOT;
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    protected function configure()
34
    {
35
        $this
36
            ->setName(self::NAME)
37
            ->setDescription(self::DESCRIPTION)
38
            ->setHelp(self::HELP)
39
            ->setDefinition([
40
                new InputOption(
41
                    'source',
42
                    's',
43
                    InputOption::VALUE_REQUIRED,
44
                    'Source path.'
45
                ),
46
                new InputOption(
47
                    'target',
48
                    't',
49
                    InputOption::VALUE_REQUIRED,
50
                    'Target path.'
51
                ),
52
                new InputOption(
53
                    'force',
54
                    'f',
55
                    InputOption::VALUE_NONE,
56
                    'Force overwrite.'
57
                ),
58
            ])
59
        ;
60
    }
61
62
    /**
63
     * Executes the command
64
     */
65
    protected function execute(InputInterface $input, OutputInterface $output)
66
    {
67
        /**
68
         * Strange issue
69
         * Without this we can't use $this->getHelper('name')
70
         * but $this->getApplication()->getHelperSet()->get('name')
71
         */
72
        $this->setApplication($this->getApplication());
73
        $filesystem = $this->getHelper('filesystem');
74
        $logger = $this->getHelper('logger');
75
76
        $source = $input->getOption('source');
77
        $target = $input->getOption('target');
78
        $overwrite = $input->getOption('force');
79
80
        try {
81
            $filesystem->copy($source, $target, $overwrite);
0 ignored issues
show
Bug introduced by
The method copy() 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...
82
            if ($output->getVerbosity() >= OutputInterface::VERBOSITY_NORMAL) {
83
                $output->writeln(sprintf('Copied <info>%s</info> to <info>%s</info>', $source, $target));
84
            }
85
            $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...
86
                self::class,
87
                [
88
                    'source' => $source,
89
                    'target' => $target,
90
                    'overwrite' => $overwrite,
91
                ]
92
            );
93
        } catch (\Exception $exception) {
94
            if (! $output->isQuiet()) {
95
                $output->writeln(sprintf('<error>%s</error>', $exception->getMessage()));
96
            }
97
            $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...
98
                self::class,
99
                [
100
                    'code' => $exception->getCode(),
101
                    'message' => $exception->getMessage(),
102
                    'source' => $source,
103
                    'target' => $target,
104
                    'overwrite' => $overwrite,
105
                ]
106
            );
107
108
            return 1;
109
        }
110
111
        return 0;
112
    }
113
}
114