HttpExceptionHandler   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 17
c 3
b 0
f 0
dl 0
loc 37
ccs 13
cts 14
cp 0.9286
rs 10
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 35 2
1
<?php
2
declare(strict_types=1);
3
4
namespace MarcinOrlowski\ResponseBuilder\ExceptionHandlers;
5
6
/**
7
 * Laravel API Response Builder
8
 *
9
 * @package   MarcinOrlowski\ResponseBuilder
10
 *
11
 * @author    Marcin Orlowski <mail (#) marcinOrlowski (.) com>
12
 * @copyright 2016-2021 Marcin Orlowski
13
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
14
 * @link      https://github.com/MarcinOrlowski/laravel-api-response-builder
15
 */
16
17
use MarcinOrlowski\ResponseBuilder\BaseApiCodes;
18
use MarcinOrlowski\ResponseBuilder\Contracts\ExceptionHandlerContract;
19
use MarcinOrlowski\ResponseBuilder\ResponseBuilder as RB;
20
use Symfony\Component\HttpFoundation\Response as HttpResponse;
21
22
/**
23
 * Handles HttpException
24 1
 */
25
final class HttpExceptionHandler implements ExceptionHandlerContract
26
{
27
	public function handle(array $user_config, \Throwable $ex): ?array
28 1
	{
29 1
		$default_config = [
30
			// used by unauthenticated() to obtain api and http code for the exception
31
			HttpResponse::HTTP_UNAUTHORIZED         => [
32 1
				'api_code' => BaseApiCodes::EX_AUTHENTICATION_EXCEPTION(),
33 1
			],
34
			// Required by ValidationException handler
35
			HttpResponse::HTTP_UNPROCESSABLE_ENTITY => [
36
				'api_code' => BaseApiCodes::EX_VALIDATION_EXCEPTION(),
37 1
			],
38 1
39
			// Default entry MUST exists. Enforced by unit tests.
40
			RB::KEY_DEFAULT            => [
41
				'api_code' => BaseApiCodes::EX_HTTP_EXCEPTION(),
42 1
			],
43
		];
44 1
45 1
		$config = \array_replace($default_config, $user_config);
46
47
		$http_code = $ex->getStatusCode();
0 ignored issues
show
Bug introduced by
The method getStatusCode() does not exist on Throwable. It seems like you code against a sub-type of Throwable such as Symfony\Component\HttpKe...\HttpExceptionInterface or Composer\Downloader\TransportException or Symfony\Component\HttpKe...Exception\HttpException. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
		/** @scrutinizer ignore-call */ 
48
  $http_code = $ex->getStatusCode();
Loading history...
48
		$result = $config[ $http_code ] ?? null;
49 1
50
		// If we do not have dedicated entry fort this particular http_code,
51
		// fall back to default value.
52
		if ($result === null) {
53
			$result = $config[ RB::KEY_DEFAULT ];
54
		}
55 1
56 1
		// Some defaults to fall back to if not set in user config.
57
		$fallback = [
58 1
			RB::KEY_HTTP_CODE => $http_code,
59
			RB::KEY_MSG_KEY   => \sprintf('response-builder::builder.http_%d', $http_code),
60
		];
61
		return \array_replace($fallback, $result);
62
	}
63
}
64