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

HoneybadgerInstallCommand::handle()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 9.52
c 0
b 0
f 0
cc 3
nc 4
nop 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()
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()
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 void
105
     */
106
    private function sendTest()
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()
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
    public function publishConfig()
145
    {
146
        if (app('honeybadger.isLumen')) {
147
            return $this->installer->publishLumenConfig();
148
        }
149
150
        return $this->installer->publishLaravelConfig();
151
    }
152
153
    private function outputSuccessMessage($noticeId)
154
    {
155
        if ($noticeId) {
156
            return $this->line(SuccessMessage::withLinkToNotice($noticeId));
157
        }
158
159
        return $this->line(SuccessMessage::withoutLinkToNotices());
160
    }
161
}
162