Completed
Branch v4.x (712f3d)
by Dmitry
04:56
created

ErrorResponseException::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 6
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Gladyshev\Yandex\Direct\Exception;
5
6
class ErrorResponseException extends \RuntimeException
7
{
8
    /**
9
     * @var string
10
     */
11
    protected $detail;
12
13
    /**
14
     * @var \Psr\Http\Message\RequestInterface
15
     */
16
    protected $request;
17
18
    /**
19
     * @var \Psr\Http\Message\ResponseInterface
20
     */
21
    protected $response;
22
23
    /**
24
     * @var array
25
     */
26
    protected $error = [];
27
28
    public function __construct(
29
        string $message,
30
        string $detail,
31
        int $code,
32
        \Psr\Http\Message\RequestInterface $request,
33
        \Psr\Http\Message\ResponseInterface $response,
34
        \Throwable $previous = null
35
    ) {
36
        $this->detail = $detail;
37
        $this->request = $request;
38
        $this->response = $response;
39
40
        parent::__construct(
41
            $message,
42
            $code,
43
            $previous
44
        );
45
    }
46
47
    /**
48
     * @return \Psr\Http\Message\RequestInterface
49
     */
50
    public function getRequest(): \Psr\Http\Message\RequestInterface
51
    {
52
        return $this->request;
53
    }
54
55
    /**
56
     * @return \Psr\Http\Message\ResponseInterface
57
     */
58
    public function getResponse(): \Psr\Http\Message\ResponseInterface
59
    {
60
        return $this->response;
61
    }
62
63
    /**
64
     * @return string
65
     */
66
    public function getDetail(): string
67
    {
68
        return $this->detail;
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    public function __toString(): string
75
    {
76
        $str = 'Exception ' . __CLASS__ . " code {$this->code} with message '{$this->message}' in `{$this->file}`" . PHP_EOL;
77
        $str .= 'Details: ' . $this->detail . PHP_EOL;
78
        $str .= 'Stack trace:' . PHP_EOL . $this->getTraceAsString() . PHP_EOL;
79
        $str .= 'Request-Response:' . PHP_EOL;
80
        $str .= '>>>' . \GuzzleHttp\Psr7\Message::toString($this->getRequest()) . PHP_EOL;
81
        $str .= '<<<' . \GuzzleHttp\Psr7\Message::toString($this->getResponse()) . PHP_EOL;
82
83
        return $str;
84
    }
85
}
86