Completed
Pull Request — master (#1)
by ANTHONIUS
02:38
created

AddCommand::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 2
1
<?php
2
3
namespace Dotfiles\Core\Command;
4
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Output\OutputInterface;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\COnsole\Input\InputOption;
9
use Dotfiles\Core\Util\Toolkit;
10
use Dotfiles\Core\Util\Filesystem;
11
12
class AddCommand extends Command implements CommandInterface
13
{
14
    protected function configure()
15
    {
16
        $this
17
            ->setName('add')
18
            ->setDescription('Add new file into dotfiles manager')
19
            ->addArgument('path', InputArgument::REQUIRED,'A file or directory name to add. This file must be exists in $HOME directory')
20
            ->addOption('machine','-m', InputOption::VALUE_OPTIONAL, 'Add this file/directory into machine registry','default')
21
            ->addOption('recursive','-r', InputOption::VALUE_NONE, 'Import all directory contents recursively')
22
        ;
23
    }
24
25
    protected function execute(InputInterface $input, OutputInterface $output)
26
    {
27
        $recursive  = $input->getOption('recursive');
0 ignored issues
show
Unused Code introduced by
The assignment to $recursive is dead and can be removed.
Loading history...
28
        $machine    = $input->getOption('machine');
29
        $dir        = getcwd()."/templates/$machine/home";
30
        $sourcePath = $input->getArgument("path");
31
        $source     = getenv("HOME").DIRECTORY_SEPARATOR.$sourcePath;
32
33
        if(!is_file($source)){
34
            throw new \InvalidArgumentException("Path <comment>$sourcePath</comment> not exists");
35
        }
36
        Toolkit::ensureDir($dir);
37
38
        $fs = new Filesystem();
39
        $fs->copy($source, $dir."/".$sourcePath);
40
    }
41
}
42