Passed
Push — master ( 7f86a3...411e4e )
by Samuel
01:33
created

anonymous//src/ClientHandler.php$0   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 13
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
dl 0
loc 13
rs 10
c 0
b 0
f 0
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(ClientException $exception)
11
    {
12
        $statusCode = 500;
13
        $title = 'client_exception';
14
        $code = config($this->configFile.'codes.client.default');
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

14
        $code = /** @scrutinizer ignore-call */ config($this->configFile.'codes.client.default');
Loading history...
Bug Best Practice introduced by
The property configFile does not exist on SMartins\JsonHandler\ClientHandler. Did you maybe forget to declare it?
Loading history...
15
        $detail = __('exception::exceptions.client.unavailable');
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

15
        $detail = /** @scrutinizer ignore-call */ __('exception::exceptions.client.unavailable');
Loading history...
16
17
        $requestHost = $exception->getRequest()->getUri()->getHost();
18
        $clientCausers = $this->clientExceptionCausers();
19
20
        if ($clientCausers->isPagarme($requestHost)) {
21
            $code = config('json-exception-handler.codes.client.pagarme') ?? 'pagarme';
22
        } elseif ($clientCausers->isMailgun($requestHost)) {
23
            $code = config('json-exception-handler.codes.client.mailgun') ?? 'mailgun';
24
        }
25
26
        if (App::environment('production')) {
27
            $detail = $detail.' #'.$code;
28
        } else {
29
            $response = $exception->getResponse();
30
            $detail = json_decode($response->getBody())->message;
31
            $statusCode = $response->getStatusCode();
32
        }
33
34
        $error = [[
35
            'status'    => $statusCode,
36
            'code'      => $code,
37
            'source'    => ['pointer' => $exception->getFile().':'.$exception->getLine()],
38
            'title'     => $title,
39
            'detail'    => $detail,
40
        ]];
41
42
        $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...
43
        $this->jsonApiResponse->setErrors($error);
44
    }
45
46
    public function clientExceptionCausers()
47
    {
48
        return new class() {
49
            const PAGARME_HOST = 'api.pagar.me';
50
51
            const MAILGUN_HOST = 'api.mailgun.net';
52
53
            public function isPagarme($host)
54
            {
55
                return self::PAGARME_HOST == $host;
56
            }
57
58
            public function isMailgun($host)
59
            {
60
                return self::MAILGUN_HOST == $host;
61
            }
62
        };
63
    }
64
}
65