Completed
Push — dev ( 0ee1b7...c0bb06 )
by Marcin
18s queued 13s
created

HttpExceptionHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
dl 0
loc 36
rs 10
c 1
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 34 4
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-2020 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;
20
use Symfony\Component\HttpFoundation\Response as HttpResponse;
21
22
final class HttpExceptionHandler implements ExceptionHandlerContract
23
{
24
	public function handle(array $user_config, \Exception $ex): ?array
25
	{
26
		$default_config = [
27
			// used by unauthenticated() to obtain api and http code for the exception
28
			HttpResponse::HTTP_UNAUTHORIZED         => [
29
				'api_code' => /** @scrutinizer ignore-deprecated */ BaseApiCodes::EX_AUTHENTICATION_EXCEPTION(),
30
			],
31
			// Required by ValidationException handler
32
			HttpResponse::HTTP_UNPROCESSABLE_ENTITY => [
33
				'api_code' => /** @scrutinizer ignore-deprecated */ BaseApiCodes::EX_VALIDATION_EXCEPTION(),
34
			],
35
36
			// Default entry MUST exists. Enforced by unit tests.
37
			ResponseBuilder::KEY_DEFAULT            => [
38
				'api_code' => BaseApiCodes::EX_HTTP_EXCEPTION(),
0 ignored issues
show
Deprecated Code introduced by
The function MarcinOrlowski\ResponseB...es::EX_HTTP_EXCEPTION() has been deprecated: Configure Exception Handler to use your own API code. This method will be removed in v8. ( Ignorable by Annotation )

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

38
				'api_code' => /** @scrutinizer ignore-deprecated */ BaseApiCodes::EX_HTTP_EXCEPTION(),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
39
			],
40
		];
41
42
		$config = \array_replace($default_config, $user_config);
43
44
		$http_code = $ex->getStatusCode();
0 ignored issues
show
Bug introduced by
The method getStatusCode() does not exist on Exception. It seems like you code against a sub-type of Exception such as 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

44
		/** @scrutinizer ignore-call */ 
45
  $http_code = $ex->getStatusCode();
Loading history...
45
		$result = $config[ $http_code ] ?? null;
46
47
		if ($result === null) {
48
			$result = $config[ ResponseBuilder::KEY_DEFAULT ];
49
		}
50
51
		if (!\array_key_exists(ResponseBuilder::KEY_HTTP_CODE, $result)) {
52
			$result[ ResponseBuilder::KEY_HTTP_CODE ] = $http_code;
53
		}
54
		if (\array_key_exists(ResponseBuilder::KEY_MSG_KEY, $result)) {
0 ignored issues
show
Deprecated Code introduced by
The constant MarcinOrlowski\ResponseB...uilderBase::KEY_MSG_KEY has been deprecated: Use KEY_MESSAGE? ( Ignorable by Annotation )

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

54
		if (\array_key_exists(/** @scrutinizer ignore-deprecated */ ResponseBuilder::KEY_MSG_KEY, $result)) {

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
55
			$result[ ResponseBuilder::KEY_MSG_KEY ] = \sprintf('response-builder::builder.http_%d', $http_code);
0 ignored issues
show
Deprecated Code introduced by
The constant MarcinOrlowski\ResponseB...uilderBase::KEY_MSG_KEY has been deprecated: Use KEY_MESSAGE? ( Ignorable by Annotation )

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

55
			$result[ /** @scrutinizer ignore-deprecated */ ResponseBuilder::KEY_MSG_KEY ] = \sprintf('response-builder::builder.http_%d', $http_code);

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
56
		}
57
		return $result;
58
	}
59
}
60