Completed
Push — master ( 2dcfbc...02d4fe )
by Petre
12:52
created

AirbrakeService::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 14
cts 14
cp 1
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 25
nc 1
nop 11
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
declare(strict_types = 1);
3
4
5
namespace SM\AirbrakeBundle\Service;
6
7
use Airbrake\ErrorHandler;
8
use Airbrake\Instance;
9
use Airbrake\Notifier;
10
use SM\AirbrakeBundle\Builder\NotifierBuilder;
11
use SM\AirbrakeBundle\Exception\AirbrakeConnectionException;
12
13
/**
14
 * Defines the Airbrake service wrapper.
15
 *
16
 * @package SM\AirbrakeBundle\Service
17
 * @author  Petre Pătrașc <[email protected]>
18
 */
19
class AirbrakeService
20
{
21
    /**
22
     * Notifier that has been set up with configuration parameters
23
     * and filters.
24
     *
25
     * @var Notifier
26
     */
27
    protected $notifier;
28
29
    /**
30
     * The global error and exception handler instance.
31
     * Defaults to null if the global_error_and_exception_handler
32
     * configuration is set to false.
33
     *
34
     * @var ErrorHandler
35
     */
36
    protected $errorHandler;
37
38
    /**
39
     * AirbrakeService constructor.
40
     *
41
     * @param NotifierBuilder $notifierBuilder
42
     * @param string          $projectId
43
     * @param string          $projectKey
44
     * @param bool            $globalExceptionInstance
45
     * @param bool            $globalErrorAndExceptionHandler
46
     * @param string          $host
47
     * @param string          $httpClient
48
     * @param string          $rootDirectory
49
     * @param array           $ignoredExceptions
50
     * @param string          $environment
51
     * @param string          $appVersion
52
     */
53 5
    public function __construct(
54
        NotifierBuilder $notifierBuilder,
55
        string $projectId,
56
        string $projectKey,
57
        bool $globalExceptionInstance,
58
        bool $globalErrorAndExceptionHandler,
59
        string $host,
60
        string $httpClient,
61
        string $rootDirectory,
62
        array $ignoredExceptions,
63
        string $environment,
64
        string $appVersion
65
    )
66
    {
67
68 4
        $this->notifier = $notifierBuilder
69 5
            ->withProjectKey($projectKey)
70 5
            ->withProjectId($projectId)
71 4
            ->withHost($host)
72 4
            ->withHttpClient($httpClient)
73 4
            ->withIgnoredExceptions($ignoredExceptions)
74 4
            ->withRootDirectory($rootDirectory)
75 4
            ->withEnvironment($environment)
76 4
            ->withAppVersion($appVersion)
77 4
            ->build();
78
79
        $this
80 4
            ->withGlobalExceptionInstance($globalExceptionInstance)
81 4
            ->withGlobalErrorAndExceptionHandler($globalErrorAndExceptionHandler);
82 4
    }
83
84
    /**
85
     * Notify Airbrake of the exception.
86
     *
87
     * @param \Exception $exception
88
     *
89
     * @return bool
90
     * @throws AirbrakeConnectionException
91
     */
92 2
    public function notify(\Exception $exception): bool
93
    {
94 2
        $airbrakeResponse = $this->notifier->notify($exception);
95
96 2
        if (false === is_array($airbrakeResponse)) {
97
            throw new AirbrakeConnectionException('There has been an error comunicating with the Airbrake service.');
98
        }
99
100 2
        if (array_key_exists('id', $airbrakeResponse)) {
101 1
            return true;
102
        }
103
104 1
        throw new AirbrakeConnectionException($airbrakeResponse['error']);
105
    }
106
107
    /**
108
     * @return Notifier
109
     */
110 1
    public function getNotifier(): Notifier
111
    {
112 1
        return $this->notifier;
113
    }
114
115
    /**
116
     * @return ErrorHandler|null
117
     */
118 2
    public function getErrorHandler()
119
    {
120 2
        return $this->errorHandler;
121
    }
122
123
    /**
124
     * Add a global exception instance if applicable.
125
     *
126
     * @param bool $globalExceptionInstance
127
     *
128
     * @return AirbrakeService
129
     */
130 4
    protected function withGlobalExceptionInstance(bool $globalExceptionInstance): AirbrakeService
131
    {
132 4
        if (true === $globalExceptionInstance) {
133 4
            Instance::set($this->notifier);
134
        }
135
136 4
        return $this;
137
    }
138
139
    /**
140
     * Add a global error and exception handler if applicable.
141
     *
142
     * @param bool $globalErrorAndExceptionHandler
143
     *
144
     * @return AirbrakeService
145
     */
146 4
    protected function withGlobalErrorAndExceptionHandler(bool $globalErrorAndExceptionHandler): AirbrakeService
147
    {
148 4
        if (true === $globalErrorAndExceptionHandler) {
149 1
            $this->errorHandler = new ErrorHandler($this->notifier);
150 1
            $this->errorHandler->register();
151
        }
152
153 4
        return $this;
154
    }
155
}
156