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:18
created

HoneybadgerInstallCommand   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 6
dl 0
loc 161
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 25 3
A gatherConfig() 0 7 1
A promptForApiKey() 0 4 1
A sendTest() 0 13 1
A writeEnv() 0 18 1
A publishConfig() 0 8 2
A outputSuccessMessage() 0 8 2
1
<?php
2
3
namespace Honeybadger\HoneybadgerLaravel\Commands;
4
5
use Honeybadger\Honeybadger;
6
use Illuminate\Console\Command;
7
use Illuminate\Support\Facades\Config;
8
use Honeybadger\HoneybadgerLaravel\CommandTasks;
9
use Honeybadger\HoneybadgerLaravel\Contracts\Installer;
10
use Honeybadger\HoneybadgerLaravel\Concerns\RequiredInput;
11
12
class HoneybadgerInstallCommand extends Command
13
{
14
    use RequiredInput;
15
16
    /**
17
     * The name and signature of the console command.
18
     *
19
     * @var string
20
     */
21
    protected $signature = 'honeybadger:install {apiKey?}';
22
23
    /**
24
     * The console command description.
25
     *
26
     * @var string
27
     */
28
    protected $description = 'Install and configure Honeybadger';
29
30
    /**
31
     * Configuration from gathered input.
32
     *
33
     * @var array
34
     */
35
    protected $config = [];
36
37
    /**
38
     * @var \Honeybadger\HoneybadgerLaravel\Contracts\Installer;
39
     */
40
    protected $installer;
41
42
    /**
43
     * @var \Honeybadger\HoneybadgerLaravel\CommandTasks
44
     */
45
    protected $tasks;
46
47
    /**
48
     * Execute the console command.
49
     *
50
     * @return mixed
51
     */
52
    public function handle(Installer $installer, CommandTasks $commandTasks)
53
    {
54
        $this->installer = $installer;
55
        $this->tasks = $commandTasks;
56
        $this->tasks->setOutput($this->output);
57
58
        $this->config = $this->gatherConfig();
59
60
        $this->writeEnv();
61
62
        if ($this->installer->shouldPublishConfig()) {
63
            $this->tasks->addTask(
64
                'Publish the config file',
65
                $this->publishConfig()
66
            );
67
        }
68
69
        if ($this->config['send_test']) {
70
            $results = $this->sendTest();
71
        }
72
73
        $this->tasks->outputResults();
74
75
        $this->outputSuccessMessage(array_get($results ?? [], 'id', ''));
76
    }
77
78
    /**
79
     * Prompt for input and gather responses.
80
     *
81
     * @return array
82
     */
83
    private function gatherConfig() : array
84
    {
85
        return [
86
            'api_key' => $this->argument('apiKey') ?? $this->promptForApiKey(),
87
            'send_test' => $this->confirm('Would you like to send a test exception now?', true),
88
        ];
89
    }
90
91
    /**
92
     * Prompt for the API key.
93
     *
94
     * @return string
95
     */
96
    private function promptForApiKey() : string
97
    {
98
        return $this->requiredSecret('Your API key', 'The API key is required');
99
    }
100
101
    /**
102
     * Send test exception to Honeybadger.
103
     *
104
     * @return array
105
     */
106
    private function sendTest() : array
107
    {
108
        Config::set('honeybadger.api_key', $this->config['api_key']);
109
110
        $result = $this->installer->sendTestException();
111
112
        $this->tasks->addTask(
113
            'Send test exception to Honeybadger',
114
            ! empty($result)
115
        );
116
117
        return $result;
118
    }
119
120
    /**
121
     * Write configuration values to the env files.
122
     *
123
     * @return void
124
     */
125
    private function writeEnv() : void
126
    {
127
        $this->tasks->addTask(
128
            'Write HONEYBADGER_API_KEY to .env',
129
            $this->installer->writeConfig(
130
                ['HONEYBADGER_API_KEY' => $this->config['api_key']],
131
                base_path('.env')
132
            )
133
        );
134
135
        $this->tasks->addTask(
136
            'Write HONEYBADGER_API_KEY placeholder to .env.example',
137
            $this->installer->writeConfig(
138
                ['HONEYBADGER_API_KEY' => ''],
139
                base_path('.env.example')
140
            )
141
        );
142
    }
143
144
    /**
145
     * Publish the config file for Lumen or Laravel.
146
     *
147
     * @return bool
148
     */
149
    public function publishConfig() : bool
150
    {
151
        if (app('honeybadger.isLumen')) {
152
            return $this->installer->publishLumenConfig();
153
        }
154
155
        return $this->installer->publishLaravelConfig();
156
    }
157
158
    /**
159
     * Output the success message.
160
     *
161
     * @param  string  $noticeId
162
     * @return void
163
     */
164
    private function outputSuccessMessage(string $noticeId) : void
165
    {
166
        if ($noticeId) {
167
            $this->line(SuccessMessage::withLinkToNotice($noticeId));
168
        } else {
169
            $this->line(SuccessMessage::withoutLinkToNotices());
170
        }
171
    }
172
}
173