AirbrakeServiceProvider   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 6 1
B register() 0 29 2
A filterEnvKey() 0 6 2
A getEnvFile() 0 8 2
A getEnvKeyFromLine() 0 4 1
A getEnvKeys() 0 8 1
A setEnvName() 0 4 1
1
<?php
2
3
namespace adamtester\laravelairbrake;
4
5
use Airbrake\Notifier;
6
use Illuminate\Support\ServiceProvider;
7
8
class AirbrakeServiceProvider extends ServiceProvider
9
{
10
    /**
11
     * Bootstrap the application services.
12
     *
13
     * @return void
14
     */
15
    public function boot()
16
    {
17
        $this->publishes([
18
            __DIR__.'/../config/airbrake.php' => config_path('airbrake.php'),
19
        ]);
20
    }
21
22
    /**
23
     * Register the application services.
24
     *
25
     * @return void
26
     */
27
    public function register()
28
    {
29
        $this->app->singleton('Airbrake\Instance', function ($app) {
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
30
            $airbrake = new Notifier([
31
                'projectId'  => config('airbrake.id'),
32
                'projectKey' => config('airbrake.key'),
33
34
                'host' => config('airbrake.host'),
35
            ]);
36
37
            $airbrake->addFilter(function ($notice) {
38
                $this->setEnvName($notice);
39
40
                foreach ($this->getEnvKeys() as $envKey) {
41
                    $this->filterEnvKey($notice, $envKey);
42
                }
43
44
                return $notice;
45
            });
46
47
            return $airbrake;
48
        });
49
50
        $handler = $this->app->make('Illuminate\Contracts\Debug\ExceptionHandler');
51
        $this->app->instance(
52
            'Illuminate\Contracts\Debug\ExceptionHandler',
53
            new Handler\AirbrakeExceptionHandler($handler, $this->app)
54
        );
55
    }
56
57
    protected function filterEnvKey(&$notice, $envKey)
58
    {
59
        if (isset($notice['environment'][$envKey])) {
60
            $notice['environment'][$envKey] = 'FILTERED';
61
        }
62
    }
63
64
    protected function getEnvFile()
65
    {
66
        $filePath = $this->app->environmentPath().'/'.$this->app->environmentFile();
0 ignored issues
show
Bug introduced by
The method environmentPath() does not exist on Illuminate\Contracts\Foundation\Application. Did you maybe mean environment()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
Bug introduced by
The method environmentFile() does not exist on Illuminate\Contracts\Foundation\Application. Did you maybe mean environment()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
67
68
        $envFile = @file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
69
70
        return is_array($envFile) ? $envFile : [];
71
    }
72
73
    protected function getEnvKeyFromLine($envLine)
74
    {
75
        return trim(current(explode('=', $envLine)));
76
    }
77
78
    protected function getEnvKeys()
79
    {
80
        $envFile = $this->getEnvFile();
81
82
        $envKeys = array_map([$this, 'getEnvKeyFromLine'], $envFile);
83
84
        return $envKeys;
85
    }
86
87
    protected function setEnvName(&$notice)
88
    {
89
        $notice['context']['environment'] = env('APP_ENV');
90
    }
91
}
92