CreateRepoCommand::execute()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 38
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 21
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 38
rs 9.584
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Thruster\Tool\ProjectGenerator\Console\Command;
6
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Process\Process;
12
13
/**
14
 * Class CreateRepoCommand.
15
 *
16
 * @author  Aurimas Niekis <[email protected]>
17
 */
18
class CreateRepoCommand extends Command
19
{
20
    protected function configure(): void
21
    {
22
        $this->setName('create:repo')
23
            ->addOption(
24
                'organisation',
25
                'o',
26
                InputOption::VALUE_OPTIONAL,
27
                'GitHub Organisation Name',
28
                'ThrusterIO'
29
            )
30
            ->addOption(
31
                'private',
32
                'p',
33
                InputOption::VALUE_NONE,
34
                'Create a private repository',
35
            );
36
    }
37
38
    protected function execute(InputInterface $input, OutputInterface $output): int
39
    {
40
        $workDir = getcwd();
41
42
        if (false === file_exists($workDir . '/composer.json')) {
43
            $output->writeln('<error>Not a project directory (composer.json is missing)</error>');
44
45
            return 1;
46
        }
47
48
        $composerJson = json_decode(file_get_contents($workDir . '/composer.json'), true);
49
50
        [$vendor, $name] = explode('/', $composerJson['name']);
51
        $organisation    = $input->getOption('organisation');
52
53
        $output->write('<info>Creating GitHub Repository: </info>');
54
        $output->writeln('<comment>' . $organisation . '/' . $name . '</comment>');
55
56
        $options = [
57
            'hub',
58
            'create',
59
        ];
60
61
        if ($input->getOption('private')) {
62
            $options[] = '-p';
63
        }
64
65
        $options[] = '-d ' . json_encode($composerJson['description']);
66
        $options[] = $organisation . '/' . $name;
67
68
        $process = new Process($options);
69
        $process->start();
70
71
        $process->wait(function ($type, $buffer): void {
72
            echo $buffer;
73
        });
74
75
        return $process->getExitCode();
76
    }
77
}
78