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

AirbrakeService::notify()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 6
cts 7
cp 0.8571
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
crap 3.0261
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