ClientHandler   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
dl 0
loc 70
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$0 ➔ isPagarme() 0 3 1
A hp$0 ➔ clientExceptionCausers() 0 15 1
clientExceptionCausers() 0 15 ?
A hp$0 ➔ isMailgun() 0 3 1
B clientException() 0 51 6
1
<?php
2
3
namespace SMartins\JsonHandler;
4
5
use Illuminate\Support\Facades\App;
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Facades\App was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use GuzzleHttp\Exception\ClientException;
0 ignored issues
show
Bug introduced by
The type GuzzleHttp\Exception\ClientException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
8
trait ClientHandler
9
{
10
    public function clientException($exception)
11
    {
12
        $statusCode = 500;
13
        $title = 'client_exception';
14
        $detail = $exception->getMessage();
15
        $code = $this->getCode();
0 ignored issues
show
Bug introduced by
It seems like getCode() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

15
        /** @scrutinizer ignore-call */ 
16
        $code = $this->getCode();
Loading history...
16
17
        if ($exception instanceof ClientException) {
18
            $requestHost = $exception->getRequest()->getUri()->getHost();
19
            $clientCausers = $this->clientExceptionCausers();
20
21
            $response = $exception->getResponse();
22
23
            if ($clientCausers->isPagarme($requestHost)) {
24
                $code = config('json-exception-handler.codes.client.pagarme') ?? 'pagarme';
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

24
                $code = /** @scrutinizer ignore-call */ config('json-exception-handler.codes.client.pagarme') ?? 'pagarme';
Loading history...
25
                $errors = json_decode($response->getBody())->errors;
26
27
                $firstErrorMessage = '';
28
                foreach ($errors as $error) {
29
                    $firstErrorMessage = $error->message;
30
                    break;
31
                }
32
33
                $detailedError = $firstErrorMessage.' #'.$code;
34
            } elseif ($clientCausers->isMailgun($requestHost)) {
35
                $code = config('json-exception-handler.codes.client.mailgun') ?? 'mailgun';
36
                $detailedError = json_decode($response->getBody())->message.' #'.$code;
37
            } else {
38
                // Unknown error
39
                $code = config('json-exception-handler.codes.client.default');
40
            }
41
42
            if (App::environment('production')) {
43
                $detail = __('exception::exceptions.client.unavailable').' #'.$code;
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

43
                $detail = /** @scrutinizer ignore-call */ __('exception::exceptions.client.unavailable').' #'.$code;
Loading history...
44
            } else {
45
                // Return more details about error
46
                $detail = $detailedError ?? $detail;
47
                $statusCode = $response->getStatusCode();
48
            }
49
        }
50
51
        $error = [[
52
            'status'    => $statusCode,
53
            'code'      => $code,
54
            'source'    => ['pointer' => $exception->getFile().':'.$exception->getLine()],
55
            'title'     => $title,
56
            'detail'    => $detail,
57
        ]];
58
59
        $this->jsonApiResponse->setStatus($statusCode);
0 ignored issues
show
Bug Best Practice introduced by
The property jsonApiResponse does not exist on SMartins\JsonHandler\ClientHandler. Did you maybe forget to declare it?
Loading history...
60
        $this->jsonApiResponse->setErrors($error);
61
    }
62
63
    public function clientExceptionCausers()
64
    {
65
        return new class() {
66
            const PAGARME_HOST = 'api.pagar.me';
67
68
            const MAILGUN_HOST = 'api.mailgun.net';
69
70
            public function isPagarme($host)
71
            {
72
                return self::PAGARME_HOST == $host;
73
            }
74
75
            public function isMailgun($host)
76
            {
77
                return self::MAILGUN_HOST == $host;
78
            }
79
        };
80
    }
81
}
82