Completed
Push — master ( 507057...03d168 )
by Lucas
02:05
created

RunCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 50
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 50
rs 9.3333
cc 1
eloc 41
nc 1
nop 0
1
<?php
2
3
namespace Kasifi\Gitaski\Command;
4
5
use Exception;
6
use Kasifi\Gitaski\GitAski;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Console\Style\SymfonyStyle;
13
use Symfony\Component\Yaml\Yaml;
14
15
class RunCommand extends Command
16
{
17
    /**
18
     * return void
19
     */
20
    protected function configure()
21
    {
22
        $this
23
            ->setName('run')
24
            ->setDescription('Build Github ASCII art')
25
            ->addArgument(
26
                'github_repository_url',
27
                InputArgument::OPTIONAL,
28
                'Which github repository URL?'
29
            )
30
            ->addOption(
31
                'use_text',
32
                null,
33
                InputOption::VALUE_REQUIRED,
34
                'If set, the ascii art will be generated from this text.'
35
            )
36
            ->addOption(
37
                'artwork_path',
38
                null,
39
                InputOption::VALUE_REQUIRED,
40
                'If set, the JSON file at this path will be used instead of the text.'
41
            )
42
            ->addOption(
43
                'input_filepath',
44
                'f',
45
                InputOption::VALUE_REQUIRED,
46
                'If set, this file will be used to generate dummy content.',
47
                realpath(__DIR__ . '/../Resources/fixtures/sample.md')
48
            )
49
            ->addOption(
50
                'commit_list_yml_filepath',
51
                null,
52
                InputOption::VALUE_REQUIRED,
53
                'If set, this file will be used to generate dummy content.',
54
                realpath(__DIR__ . '/../Resources/fixtures/commit-messages.yml')
55
            )
56
            ->addOption(
57
                'output_filename',
58
                null,
59
                InputOption::VALUE_REQUIRED,
60
                'If set, this filename will be used for the commited file.',
61
                'README.md'
62
            )
63
            ->addOption(
64
                'force',
65
                null,
66
                InputOption::VALUE_NONE,
67
                'If set, the commits will be really pushed to the repository URL, else it run in dry-mode mode.'
68
            );
69
    }
70
71
    /**
72
     * @param InputInterface  $input
73
     * @param OutputInterface $output
74
     *
75
     * @return void
76
     * @throws Exception
77
     */
78
    protected function execute(InputInterface $input, OutputInterface $output)
79
    {
80
        $io = new SymfonyStyle($input, $output);
81
82
        $force = $input->getOption('force');
83
        $githubRepositoryUrl = $input->getArgument('github_repository_url');
84
        $inputFilePath = $input->getOption('input_filepath');
85
        if (!file_exists($inputFilePath)) {
86
            throw new Exception($inputFilePath . ' file was not found.');
87
        }
88
        $outputFilename = $input->getOption('output_filename');
89
90
        $commitsMessageYmlPath = $input->getOption('commit_list_yml_filepath');
91
        if (!file_exists($commitsMessageYmlPath)) {
92
            throw new Exception($commitsMessageYmlPath . ' file was not found.');
93
        }
94
        $commitMessages = Yaml::parse(file_get_contents($commitsMessageYmlPath));
95
96
        $gitaski = new GitAski($force, $githubRepositoryUrl, $inputFilePath, $outputFilename, $commitMessages);
97
        $gitaski->setIo($io);
98
99
        $useText = $input->getOption('use_text');
100
        if ($useText) {
101
            $gitaski->writeText($useText);
102
        } else {
103
            $artworkPath = $input->getOption('artwork_path');
104
            if (!$artworkPath) {
105
                throw new \InvalidArgumentException('Either "use_text" or "artwork_path" option should be defined');
106
            }
107
            $gitaski->writeJson($artworkPath);
108
        }
109
        $gitaski->clean();
110
    }
111
}