GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#11)
by TJ
02:27
created

HoneybadgerInstallCommand::handle()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 3
nc 4
nop 0
1
<?php
2
3
namespace Honeybadger\HoneybadgerLaravel\Commands;
4
5
use Honeybadger\Honeybadger;
6
use InvalidArgumentException;
7
use Illuminate\Console\Command;
8
use Illuminate\Support\Facades\Config;
9
use sixlive\DotenvEditor\DotenvEditor;
10
11
abstract class HoneybadgerInstallCommand extends Command
12
{
13
    /**
14
     * The name and signature of the console command.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'honeybadger:install {apiKey?}';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Install and configure Honeybadger';
26
27
    /**
28
     * Results of each step of the install.
29
     *
30
     * @var array
31
     */
32
    protected $results = [];
33
34
    /**
35
     * Configuration from gathered input.
36
     *
37
     * @var array
38
     */
39
    protected $config = [];
40
41
    /**
42
     * Execute the console command.
43
     *
44
     * @return mixed
45
     */
46
    public function handle()
47
    {
48
        $this->config = $this->gatherConfig();
49
50
        $this->writeEnv();
51
52
        if ($this->shouldPublishConfig()) {
53
            $this->task(
54
                'Publish the config file',
55
                $this->publishConfig()
56
            );
57
        }
58
59
        if ($this->config['send_test']) {
60
            $this->sendTest();
61
        }
62
63
        $this->outputResults();
64
    }
65
66
    /**
67
     * Publish the configuration file to the framework.
68
     *
69
     * @return bool
70
     */
71
    abstract public function publishConfig();
72
73
    /**
74
     * Prompt for input and gather responses.
75
     *
76
     * @return array
77
     */
78
    private function gatherConfig()
79
    {
80
        return [
81
            'api_key' => $this->argument('apiKey') ?? $this->secret('Please enter your API key'),
82
            'send_test' => $this->confirm('Would you like to send a test exception now?', true),
83
        ];
84
    }
85
86
    /**
87
     * Send test exception to Honeybadger.
88
     *
89
     * @return void
90
     */
91
    private function sendTest()
92
    {
93
        Config::set('honeybadger.api_key', $this->config['api_key']);
94
95
        $this->task(
96
            'Send test exception to Honeybadger',
97
            $this->callSilent('honeybadger:test') === 0
98
        );
99
    }
100
101
    /**
102
     * Write configuration values to the env files.
103
     *
104
     * @return void
105
     */
106
    private function writeEnv()
107
    {
108
        $this->task(
109
            'Write HONEYBADGER_API_KEY to .env',
110
            $this->writeConfig(['HONEYBADGER_API_KEY' => $this->config['api_key']])
111
        );
112
113
        $this->task(
114
            'Write HONEYBADGER_API_KEY placeholder to .env.example',
115
            $this->writeConfig(['HONEYBADGER_API_KEY' => ''], '.env.example')
116
        );
117
    }
118
119
    /**
120
     * Whether the configuration needs to be published or no.
121
     *
122
     * @return bool
123
     */
124
    private function shouldPublishConfig()
125
    {
126
        return ! file_exists(base_path('config/honeybadger.php'));
127
    }
128
129
    /**
130
     * Write the configurations to dotenv files.
131
     *
132
     * @param  array  $config
133
     * @param  string  $file
134
     * @return bool
135
     */
136
    private function writeConfig($config, $file = '.env')
137
    {
138
        try {
139
            $env = new DotenvEditor;
140
            $env->load(base_path($file));
141
        } catch (InvalidArgumentException $e) {
142
            return false;
143
        }
144
145
        collect($config)->each(function ($value, $key) use ($env) {
146
            $env->set($key, $value);
147
        });
148
149
        return $env->save();
150
    }
151
152
    /**
153
     * Add the results of each installation step.
154
     *
155
     * @param  string  $name
156
     * @param  bool  $result
157
     * @return void
158
     */
159
    public function task($name, $result)
160
    {
161
        $this->results[$name] = $result;
162
    }
163
164
    /**
165
     * Output the results of each step of the installation.
166
     *
167
     * @return void
168
     */
169
    private function outputResults()
170
    {
171
        collect($this->results)->each(function ($result, $description) {
172
            $this->line(vsprintf('%s: %s', [
173
                $description,
174
                $result ? '<fg=green>✔</>' : '<fg=red>✘</>',
175
            ]));
176
        });
177
    }
178
}
179