RunCommand   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 6
c 3
b 0
f 0
lcom 0
cbo 5
dl 0
loc 96
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 49 1
B execute() 0 33 5
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::REQUIRED,
28
                'The github repository URL: Ex: [email protected]:you/your_fresh_repo.git'
29
            )
30
            ->addArgument(
31
                'text',
32
                InputArgument::OPTIONAL,
33
                'If set, the ascii art will be generated from this text.'
34
            )
35
            ->addOption(
36
                'artwork_path',
37
                null,
38
                InputOption::VALUE_REQUIRED,
39
                'If set, the JSON file at this path will be used instead of the text.'
40
            )
41
            ->addOption(
42
                'input_filepath',
43
                'f',
44
                InputOption::VALUE_REQUIRED,
45
                'If set, this file will be used to generate dummy content.',
46
                __DIR__ . '/../Resources/fixtures/sample.md'
47
            )
48
            ->addOption(
49
                'commit_list_yml_filepath',
50
                null,
51
                InputOption::VALUE_REQUIRED,
52
                'If set, this file will be used to generate dummy content.',
53
                __DIR__ . '/../Resources/fixtures/commit-messages.yml'
54
            )
55
            ->addOption(
56
                'output_filename',
57
                null,
58
                InputOption::VALUE_REQUIRED,
59
                'If set, this filename will be used for the commited file.',
60
                'README.md'
61
            )
62
            ->addOption(
63
                'dry-run',
64
                null,
65
                InputOption::VALUE_NONE,
66
                'If set, the commits will not be pushed to the repository URL.'
67
            );
68
    }
69
70
    /**
71
     * @param InputInterface  $input
72
     * @param OutputInterface $output
73
     *
74
     * @return void
75
     * @throws Exception
76
     */
77
    protected function execute(InputInterface $input, OutputInterface $output)
78
    {
79
        $io = new SymfonyStyle($input, $output);
80
81
        $force = !$input->getOption('dry-run');
82
        $githubRepositoryUrl = $input->getArgument('github_repository_url');
83
        $inputFilePath = $input->getOption('input_filepath');
84
        if (!file_exists($inputFilePath)) {
85
            throw new Exception('Input file: ' . $inputFilePath . ' was not found.');
86
        }
87
        $outputFilename = $input->getOption('output_filename');
88
89
        $commitsMessageYmlPath = $input->getOption('commit_list_yml_filepath');
90
        if (!file_exists($commitsMessageYmlPath)) {
91
            throw new Exception('Commit message file: ' . $commitsMessageYmlPath . ' was not found.');
92
        }
93
        $commitMessages = Yaml::parse(file_get_contents($commitsMessageYmlPath));
94
95
        $gitaski = new GitAski($force, $githubRepositoryUrl, $inputFilePath, $outputFilename, $commitMessages);
96
        $gitaski->setIo($io);
97
98
        $text = $input->getArgument('text');
99
        if ($text) {
100
            $gitaski->writeText($text);
101
        } else {
102
            $artworkPath = $input->getOption('artwork_path');
103
            if (!$artworkPath) {
104
                throw new \InvalidArgumentException('Either "use_text" or "artwork_path" option should be defined');
105
            }
106
            $gitaski->writeJson($artworkPath);
107
        }
108
        $gitaski->clean();
109
    }
110
}