SyncCommand::execute()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 2.0011

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 27
ccs 14
cts 15
cp 0.9333
rs 8.8571
cc 2
eloc 14
nc 2
nop 2
crap 2.0011
1
<?php
2
3
namespace SyncFS\Command;
4
5
use SyncFS\Configuration\Configuration;
6
use SyncFS\Configuration\Reader;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use SyncFS\Syncer;
11
12
/**
13
 * Class SyncCommand
14
 *
15
 * @package SyncFS\Command
16
 * @author  Matej Velikonja <[email protected]>
17
 */
18
class SyncCommand extends Command
19
{
20
    const COMMAND_NAME = 'sync';
21
22
    /**
23
     * Configure command.
24
     */
25 6
    protected function configure()
26
    {
27 6
        $this
28 6
            ->setName(self::COMMAND_NAME)
29 6
            ->setDescription("Run synchronization.")
30 6
            ->addArgument(
31 6
                self::ARG_CONFIG_PATH,
32 6
                InputArgument::OPTIONAL,
33 6
                'Path to config file.',
34 6
                $this->getDefaultConfiguration()
35 6
            );
36 6
    }
37
38
    /**
39
     * @param InputInterface  $input
40
     * @param OutputInterface $output
41
     *
42
     * @throws \Exception
43
     *
44
     * @return int|null|void
45
     */
46 2
    protected function execute(InputInterface $input, OutputInterface $output)
47
    {
48 2
        $configPath = $input->getArgument('config-path');
49
50 2
        if (! file_exists($configPath)) {
51
            throw new \Exception(sprintf('Configuration file %s does not exists.', $configPath));
52
        }
53
54 2
        $configPath = realpath($configPath);
55
56 2
        $output->writeln(sprintf('Reading configuration from file %s.', $configPath));
57
58 2
        $reader = new Reader(new Configuration());
59 2
        $config = $reader->read($configPath);
60
61 2
        $output->writeln(sprintf('<info>Configuration read.</info>'));
62
63 2
        $syncer = new Syncer($config);
64
65 2
        $output->writeln('<info>Syncing folders...</info>' . PHP_EOL);
66
67 2
        $syncer->sync($output);
68
69 2
        $output->writeln(
70
            '<info>Syncing folders succeeded!</info>'
71 2
        );
72 2
    }
73
}
74