Passed
Push — master ( 78e7d4...ca6e02 )
by Arnaud
05:07
created

Edit   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 57
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 12 1
A execute() 0 18 4
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Cecil.
7
 *
8
 * Copyright (c) Arnaud Ligny <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Cecil\Command;
15
16
use Cecil\Exception\RuntimeException;
17
use Cecil\Util;
18
use Symfony\Component\Console\Input\InputArgument;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Input\InputOption;
21
use Symfony\Component\Console\Output\OutputInterface;
22
23
/**
24
 * Open pages with an editor.
25
 */
26
class Edit extends AbstractCommand
27
{
28
    /**
29
     * {@inheritdoc}
30
     */
31
    protected function configure()
32
    {
33
        $this
34
            ->setName('edit')
35
            ->setAliases(['open'])
36
            ->setDescription('Open pages directory with the editor')
37
            ->setDefinition([
38
                new InputArgument('path', InputArgument::OPTIONAL, 'Use the given path as working directory'),
39
                new InputOption('editor', null, InputOption::VALUE_REQUIRED, 'Editor to use'),
40
            ])
41
            ->setHelp(
42
                <<<'EOF'
43
The <info>%command.name%</> command open pages directory with the editor defined in the configuration file.
44
45
To open pages directory with the editor, run:
46
47
  <info>%command.full_name%</>
48
49
To open pages directory with the editor from a specific directory, run:
50
51
  <info>%command.full_name% path/to/directory</>
52
53
To open pages directory with a specific editor, run:
54
55
  <info>%command.full_name% --editor=editor</>
56
EOF
57
            );
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     *
63
     * @throws RuntimeException
64
     */
65
    protected function execute(InputInterface $input, OutputInterface $output)
66
    {
67
        try {
68
            if (null === $editor = $input->getOption('editor')) {
69
                if (!$this->getBuilder()->getConfig()->has('editor')) {
70
                    $output->writeln('<comment>No editor configured.</comment>');
71
72
                    return 0;
73
                }
74
                $editor = (string) $this->getBuilder()->getConfig()->get('editor');
75
            }
76
            $output->writeln(\sprintf('<info>Opening pages directory with %s...</info>', ucfirst($editor)));
77
            $this->openEditor(Util::joinFile($this->getPath(), (string) $this->getBuilder()->getConfig()->get('pages.dir')), $editor);
78
        } catch (\Exception $e) {
79
            throw new RuntimeException(\sprintf($e->getMessage()));
80
        }
81
82
        return 0;
83
    }
84
}
85