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 (#26)
by TJ
04:29
created

HoneybadgerDeployCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 92
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A handle() 0 15 3
A resolveParams() 0 7 1
A resolveConfigValues() 0 10 1
A resolveOptions() 0 10 1
A gitHash() 0 4 1
A gitRemote() 0 4 1
1
<?php
2
3
namespace Honeybadger\HoneybadgerLaravel\Commands;
4
5
use GuzzleHttp\Client;
6
use Illuminate\Console\Command;
7
use Illuminate\Support\Facades\Config;
8
9
class HoneybadgerDeployCommand extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'honeybadger:deploy {--apiKey=} {--environment=} {--revision=} {--repository=} {--username=}';
17
18
    /**
19
     * @var \GuzzleHttp\Client
20
     */
21
    protected $client;
22
23
    /**
24
     * The console command description.
25
     *
26
     * @var string
27
     */
28
    protected $description = 'Send deployment to Honeybadger';
29
30
    /**
31
     * @var \GuzzleHttp\Client
32
     */
33
    public function __construct(Client $client)
34
    {
35
        parent::__construct();
36
37
        $this->client = $client;
38
    }
39
40
    /**
41
     * Execute the console command.
42
     *
43
     * @return mixed
44
     */
45
    public function handle()
46
    {
47
        $response = $this->client->post(
48
            'https://api.honeybadger.io/v1/deploys',
49
            [
50
                'form_params' => $this->resolveParams(),
51
            ]
52
        );
53
54
        $body = json_decode((string) $response->getBody(), true);
55
56
        if ($response->getStatusCode() !== 200 && $body['status'] === 'OK') {
57
            throw new \Exceptions("Sending the deployment to Honeybadger faild. Status Code {$response->getStatusCode()}");
58
        }
59
    }
60
61
    private function resolveParams() : array
62
    {
63
        return array_merge(
64
            $this->resolveConfigValues(),
65
            $this->resolveOptions()
66
        );
67
    }
68
69
    private function resolveConfigValues() : array
70
    {
71
        $config = Config::get('honeybadger');
72
73
        return [
74
           'api_key'  => $config['api_key'],
75
           'revision' => $config['version'] ?? $this->gitHash(),
76
           'environment' => $config['environment_name'],
77
        ];
78
    }
79
80
    private function resolveOptions() : array
81
    {
82
        return array_filter([
83
            'api_key' => $this->option('apiKey'),
84
            'environment' => $this->option('environment'),
85
            'revision' => $this->option('revision'),
86
            'repository' => $this->option('repository') ?? $this->gitRemote(),
87
            'local_username' => $this->option('username') ?? get_current_user(),
88
        ]);
89
    }
90
91
    private function gitHash() : string
92
    {
93
        return trim(exec('git log --pretty="%h" -n1 HEAD'));
94
    }
95
96
    private function gitRemote() : string
97
    {
98
        return trim(exec('git config --get remote.origin.url'));
99
    }
100
}
101