Passed
Push — fix/serve-timeout ( 697f77...233bc8 )
by Arnaud
04:27
created

OpenWith::execute()   A

Complexity

Conditions 4
Paths 11

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
c 2
b 0
f 0
nc 11
nop 2
dl 0
loc 18
rs 9.9
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\InputDefinition;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Input\InputOption;
22
use Symfony\Component\Console\Output\OutputInterface;
23
24
/**
25
 * Open content with an editor.
26
 */
27
class OpenWith extends AbstractCommand
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    protected function configure()
33
    {
34
        $this
35
            ->setName('open')
36
            ->setDescription('Open content directory with the editor')
37
            ->setDefinition(
38
                new InputDefinition([
39
                    new InputArgument('path', InputArgument::OPTIONAL, 'Use the given path as working directory'),
40
                    new InputOption('editor', null, InputOption::VALUE_REQUIRED, 'Editor to use'),
41
                ])
42
            )
43
            ->setHelp('Open content directory with the editor defined in the configuration file.');
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     *
49
     * @throws RuntimeException
50
     */
51
    protected function execute(InputInterface $input, OutputInterface $output)
52
    {
53
        try {
54
            if (null === $editor = $input->getOption('editor')) {
55
                if (!$this->getBuilder()->getConfig()->has('editor')) {
56
                    $output->writeln('<comment>No editor configured.</comment>');
57
58
                    return 0;
59
                }
60
                $editor = (string) $this->getBuilder()->getConfig()->get('editor');
61
            }
62
            $output->writeln(\sprintf('<info>Opening content directory with %s...</info>', ucfirst($editor)));
63
            $this->openEditor(Util::joinFile($this->getPath(), (string) $this->getBuilder()->getConfig()->get('content.dir')), $editor);
64
        } catch (\Exception $e) {
65
            throw new RuntimeException(\sprintf($e->getMessage()));
66
        }
67
68
        return 0;
69
    }
70
}
71