Completed
Push — master ( d88eff...7995d8 )
by Petre
02:16
created

NotifierBuilder::addFilteredExceptions()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 2
nop 1
1
<?php
2
declare(strict_types = 1);
3
4
5
namespace SM\AirbrakeBundle\Builder;
6
7
use Airbrake\Notifier;
8
use SM\AirbrakeBundle\Enum\AirbrakeDefaultEnum;
9
use SM\AirbrakeBundle\Exception\AirbrakeConfigurationException;
10
11
/**
12
 * Handles the construction of Airbrake notifiers.
13
 *
14
 * @package SM\AirbrakeBundle\Builder
15
 * @author  Petre Pătrașc <[email protected]>
16
 */
17
class NotifierBuilder implements BuilderInterface
18
{
19
    /**
20
     * @var string
21
     */
22
    protected $projectKey;
23
24
    /**
25
     * @var string
26
     */
27
    protected $projectId;
28
29
    /**
30
     * @var string
31
     */
32
    protected $host;
33
34
    /**
35
     * @var string
36
     */
37
    protected $httpClient;
38
39
    /**
40
     * @var string
41
     */
42
    protected $rootDirectory;
43
44
    /**
45
     * @var array
46
     */
47
    protected $ignoredExceptions;
48
49
    /**
50
     * @var string
51
     */
52
    protected $environment;
53
54
    /**
55
     * @var string
56
     */
57
    protected $appVersion;
58
59
    /**
60
     * NotifierBuilder constructor.
61
     */
62
    public function __construct()
63
    {
64
        $this->clear();
65
    }
66
67
    public function withProjectKey(string $projectKey): NotifierBuilder
68
    {
69
        if (empty(trim($projectKey))) {
70
            throw new AirbrakeConfigurationException('Project API key cannot be empty');
71
        }
72
73
        $this->projectKey = $projectKey;
74
75
        return $this;
76
    }
77
78
    public function withProjectId(string $projectId): NotifierBuilder
79
    {
80
        if (empty(trim($projectId))) {
81
            throw new AirbrakeConfigurationException('Project ID cannot be empty');
82
        }
83
84
        $this->projectId = $projectId;
85
86
        return $this;
87
    }
88
89 View Code Duplication
    public function withHost(string $host): NotifierBuilder
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
    {
91
        if (empty(trim($host))) {
92
            throw new AirbrakeConfigurationException('Host cannot be empty');
93
        }
94
95
        $this->host = $host;
96
97
        return $this;
98
    }
99
100
    public function withHttpClient(string $httpClient): NotifierBuilder
101
    {
102
        if (empty(trim($httpClient))) {
103
            throw new AirbrakeConfigurationException('HTTP client cannot be empty');
104
        }
105
106
        $this->httpClient = $httpClient;
107
108
        return $this;
109
    }
110
111
    public function withRootDirectory(string $rootDirectory): NotifierBuilder
112
    {
113
        if (empty(trim($rootDirectory))) {
114
            throw new AirbrakeConfigurationException('Root directory cannot be empty');
115
        }
116
117
        $this->rootDirectory = $rootDirectory;
118
119
        return $this;
120
    }
121
122
    public function withIgnoredExceptions(array $ignoredExceptions = []): NotifierBuilder
123
    {
124
        $this->ignoredExceptions = $ignoredExceptions;
125
126
        return $this;
127
    }
128
129 View Code Duplication
    public function withEnvironment(string $environment): NotifierBuilder
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
130
    {
131
        if (empty(trim($environment))) {
132
            throw new AirbrakeConfigurationException('Environment cannot be empty');
133
        }
134
135
        $this->environment = $environment;
136
137
        return $this;
138
    }
139
140
    public function withAppVersion(string $appVersion): NotifierBuilder
141
    {
142
        if (empty(trim($appVersion))) {
143
            throw new AirbrakeConfigurationException('App version cannot be empty');
144
        }
145
146
        $this->appVersion = $appVersion;
147
148
        return $this;
149
    }
150
151
    /**
152
     * @inheritDoc
153
     */
154
    public function build(): Notifier
155
    {
156
        $notifierInstance = new Notifier([
157
            'projectId'     => $this->projectId,
158
            'projectKey'    => $this->projectKey,
159
            'host'          => $this->host,
160
            'rootDirectory' => $this->rootDirectory,
161
            'httpClient'    => $this->httpClient,
162
            'environment'   => $this->environment,
163
            'appVersion'    => $this->appVersion,
164
        ]);
165
166
        $this->addFilteredExceptions($notifierInstance);
167
        $this->clear();
168
169
        return $notifierInstance;
170
    }
171
172
    /**
173
     * @inheritDoc
174
     */
175
    public function clear()
176
    {
177
        $this->projectKey        = AirbrakeDefaultEnum::PROJECT_KEY;
178
        $this->projectId         = AirbrakeDefaultEnum::PROJECT_ID;
179
        $this->host              = AirbrakeDefaultEnum::HOST;
180
        $this->httpClient        = AirbrakeDefaultEnum::HTTP_CLIENT;
181
        $this->ignoredExceptions = AirbrakeDefaultEnum::IGNORED_EXCEPTIONS;
182
        $this->rootDirectory     = AirbrakeDefaultEnum::ROOT_DIRECTORY;
183
        $this->environment       = AirbrakeDefaultEnum::ENVIRONMENT;
184
        $this->appVersion        = AirbrakeDefaultEnum::APP_VERSION;
185
    }
186
187
    /**
188
     * Add filtered exceptions to the notifier.
189
     *
190
     * @param Notifier $notifierInstance
191
     */
192
    protected function addFilteredExceptions(Notifier $notifierInstance)
193
    {
194
        if (false === empty($this->ignoredExceptions)) {
195
            $notifierInstance->addFilter(function ($notice) {
196
                foreach ($this->ignoredExceptions as $exceptionClass) {
197
                    if ($notice['errors'][0]['type'] === $exceptionClass) {
198
                        return null;
199
                    }
200
                }
201
202
                return $notice;
203
            });
204
        }
205
    }
206
}
207