Edit   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 52
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
/**
4
 * This file is part of Cecil.
5
 *
6
 * (c) Arnaud Ligny <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
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
 * Edit command.
25
 *
26
 * This command opens the pages directory with the configured editor.
27
 * It allows users to quickly access and edit their content files directly from the command line.
28
 */
29
class Edit extends AbstractCommand
30
{
31
    /**
32
     * {@inheritdoc}
33
     */
34
    protected function configure()
35
    {
36
        $this
37
            ->setName('edit')
38
            ->setAliases(['open'])
39
            ->setDescription('Open pages directory with the editor')
40
            ->setDefinition([
41
                new InputArgument('path', InputArgument::OPTIONAL, 'Use the given path as working directory'),
42
                new InputOption('editor', null, InputOption::VALUE_REQUIRED, 'Editor to use'),
43
            ])
44
            ->setHelp(
45
                <<<'EOF'
46
The <info>%command.name%</> command open pages directory with the <comment>editor defined</comment> in the configuration file.
47
48
  <info>%command.full_name%</>
49
  <info>%command.full_name% path/to/the/working/directory</>
50
51
To open pages directory with a <comment>specific editor</comment>, run:
52
53
  <info>%command.full_name% --editor=editor</>
54
EOF
55
            );
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     *
61
     * @throws RuntimeException
62
     */
63
    protected function execute(InputInterface $input, OutputInterface $output)
64
    {
65
        try {
66
            if (null === $editor = $input->getOption('editor')) {
67
                if (!$this->getBuilder()->getConfig()->has('editor')) {
68
                    $output->writeln('<comment>No editor configured.</comment>');
69
70
                    return 0;
71
                }
72
                $editor = (string) $this->getBuilder()->getConfig()->get('editor');
73
            }
74
            $output->writeln(\sprintf('<info>Opening pages directory with %s...</info>', ucfirst($editor)));
75
            $this->openEditor(Util::joinFile($this->getPath(), (string) $this->getBuilder()->getConfig()->get('pages.dir')), $editor);
76
        } catch (\Exception $e) {
77
            throw new RuntimeException(\sprintf($e->getMessage()));
78
        }
79
80
        return 0;
81
    }
82
}
83