GithubInitCommand::gitHubUsername()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 2
eloc 3
nc 2
nop 2
1
<?php
2
3
namespace Acacha\Llum\Console;
4
5
use Acacha\Llum\Parser\LlumRCParser;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
/**
11
 * Class GithubInitCommand.
12
 *
13
 * @package Acacha\Llum\Console
14
 */
15
class GithubInitCommand extends LlumCommand
16
{
17
18
    use GithubCommand;
19
20
    /**
21
     * Symfony console output.
22
     *
23
     * @var OutputInterface
24
     */
25
    protected $output;
26
27
    /**
28
     * Llum rc file parser.
29
     *
30
     * @var LlumRCParser
31
     */
32
    protected $parser;
33
34
    /**
35
     * Command name.
36
     *
37
     * @var string
38
     */
39
    protected $commandName = 'github:init';
40
41
    /**
42
     * Command description.
43
     *
44
     * @var string
45
     */
46
    protected $commandDescription = 'Initializes current folder as a new git project';
47
48
    /**
49
     * Method to execute.
50
     *
51
     * @var string
52
     */
53
    protected $method = 'gitHubInitRepo';
54
55
    /**
56
     * GithubInitCommand constructor.
57
     *
58
     * @param LlumRCParser $parser
59
     */
60
    public function __construct(LlumRCParser $parser)
61
    {
62
        parent::__construct();
63
        $this->parser = $parser;
64
    }
65
66
    /**
67
     * Github init repo command.
68
     *
69
     * @param InputInterface $input
70
     * @param OutputInterface $output
71
     */
72
    public function gitHubInitRepo(InputInterface $input, OutputInterface $output)
73
    {
74
        $this->output = $output;
75
        $this->runGitInit();
76
        $this->runGitAdd();
77
        $this->runGitCommit();
78
        $this->runGitCreateRepo();
79
        $this->runGitRemoteAddOrigin($this->getRepoURL($input,$output));
80
        $this->runGitPull();
81
        $this->runGitPush();
82
    }
83
84
85
    /**
86
     * Get github repo URL.
87
     *
88
     * @param OutputInterface $output
89
     * @param InputInterface $input
90
     * @return string
91
     */
92
    public function getRepoURL(InputInterface $input, OutputInterface $output)
93
    {
94
        return '[email protected]:' . $this->gitHubUsername($input,$output) . '/' . $this->repoName($input) . '.git';
95
    }
96
97
    /**
98
     * Obtain repo name.
99
     *
100
     * @param InputInterface $input
101
     * @return mixed|string
102
     */
103
    protected function repoName(InputInterface $input) {
104
        $name = $input->getArgument('name');
105
        return isset($name) ? $name : basename(getcwd());
106
    }
107
108
109
    /**
110
     * Get github username.
111
     *
112
     * @param InputInterface $input
113
     * @param OutputInterface $output
114
     * @return array|mixed
115
     */
116
    protected function gitHubUsername(InputInterface $input, OutputInterface $output)
117
    {
118
        $username = $input->getArgument('github_username');
119
        return isset($username) ? $username : $this->getGithubUserNameFromConfig($output);
120
    }
121
122
    /**
123
     * Get github username from llum config.
124
     *
125
     * @param OutputInterface $output
126
     * @return array
127
     */
128 View Code Duplication
    protected function getGithubUserNameFromConfig(OutputInterface $output)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
    {
130
        $username = $this->parser->getGitHubUsername();
131
        if (is_null($username)) {
132
            $this->showErrorRunLlumInitFirst($output,'username');
133
        }
134
        return $username;
135
    }
136
137
    /**
138
     * Run git init command.
139
     */
140
    protected function runGitInit(){
141
        $this->showMessageRunningCommand($this->output,'git init');
142
        passthru('git init');
143
    }
144
145
    /**
146
     * Run git add command.
147
     */
148
    protected function runGitAdd(){
149
        $this->showMessageRunningCommand($this->output,'git add .');
150
        passthru('git add .');
151
    }
152
153
    /**
154
     * Run git commit command.
155
     */
156
    protected function runGitCommit(){
157
        $this->showMessageRunningCommand($this->output,'git commit -a -m "Initial version"');
158
        passthru('git commit -a -m "Initial version"');
159
    }
160
161
    /**
162
     * Run llum github:repo command.
163
     */
164
    protected function runGitCreateRepo()
165
    {
166
        $this->showMessageRunningCommand($this->output,'llum github:repo');
167
        passthru('llum github:repo');
168
    }
169
170
    /**
171
     * Run git remote add origin.
172
     *
173
     * @param $url
174
     */
175
    protected function runGitRemoteAddOrigin($url){
176
        $this->showMessageRunningCommand($this->output,'git remote add origin ' . $url);
177
        passthru('git remote add origin ' . $url);
178
    }
179
180
    /**
181
     * Run git pull.
182
     */
183
    protected function runGitPull(){
184
        $this->showMessageRunningCommand($this->output,'git pull origin master');
185
        passthru('git pull origin master');
186
    }
187
188
    /**
189
     * Run git push.
190
     */
191
    protected function runGitPush(){
192
        $this->showMessageRunningCommand($this->output,'git push origin master');
193
        passthru('git push origin master');
194
    }
195
196
    /**
197
     * Configure command.
198
     */
199
    public function configure()
200
    {
201
        parent::configure();
202
203
        $this
204
            // configure an argument
205
            ->addArgument('name', InputArgument::OPTIONAL, 'Repository name')
206
            ->addArgument('github_username', InputArgument::OPTIONAL, 'Github username')
207
        ;
208
    }
209
}