Completed
Push — master ( 45165d...5e44c4 )
by Alexey
04:08 queued 11s
created

GooglePlayException::getRequest()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 9
ccs 0
cts 5
cp 0
rs 10
cc 3
nc 2
nop 0
crap 12
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author   Ne-Lexa
7
 * @license  MIT
8
 *
9
 * @see      https://github.com/Ne-Lexa/google-play-scraper
10
 */
11
12
namespace Nelexa\GPlay\Exception;
13
14
use GuzzleHttp\Exception\RequestException;
15
use Psr\Http\Message\RequestInterface;
16
use Psr\Http\Message\ResponseInterface;
17
18
/**
19
 * Exception thrown if there is an error accessing the Google Play server.
20
 */
21
class GooglePlayException extends \Exception
22
{
23
    /** @var string|null URL associated with the exception. */
24
    private $url;
25
26
    /**
27
     * Construct the GooglePlayException.
28
     *
29
     * @param string     $message  the Exception message to throw
30
     * @param int        $code     the Exception code
31
     * @param \Throwable $previous the previous throwable used for the exception chaining
32
     */
33 8
    public function __construct($message = '', $code = 0, ?\Throwable $previous = null)
34
    {
35 8
        parent::__construct($message, $code, $previous);
36
37 8
        if ($previous instanceof RequestException) {
38 5
            $this->url = $previous->getRequest()->getUri()->__toString();
39
        }
40 8
    }
41
42
    /**
43
     * Set the URL associated with the exception.
44
     *
45
     * @param string $url URL associated with the exception
46
     *
47
     * @return GooglePlayException returns the same object to support the call chain
48
     */
49 2
    public function setUrl(string $url): self
50
    {
51 2
        $this->url = $url;
52
53 2
        return $this;
54
    }
55
56
    /**
57
     * Returns the URL with which the exception is associated.
58
     *
59
     * URL maybe `null`.
60
     *
61
     * @return string|null URL associated with the exception or `null`
62
     */
63
    public function getUrl(): ?string
64
    {
65
        return $this->url;
66
    }
67
68
    /**
69
     * Returns an HTTP request if present.
70
     *
71
     * @return RequestInterface|null PSR-7 `RequestInterface` or `null`
72
     *
73
     * @see https://www.php-fig.org/psr/psr-7/ PSR-7: HTTP message interfaces
74
     */
75
    public function getRequest(): ?RequestInterface
76
    {
77
        $e = $this->getPrevious();
78
79
        if ($e instanceof RequestException && $e->getRequest() !== null) {
80
            return $e->getRequest();
81
        }
82
83
        return null;
84
    }
85
86
    /**
87
     * Returns an HTTP response if present.
88
     *
89
     * @return ResponseInterface|null PSR-7 `ResponseInterface` or `null`
90
     *
91
     * @see https://www.php-fig.org/psr/psr-7/ PSR-7: HTTP message interfaces
92
     */
93
    public function getResponse(): ?ResponseInterface
94
    {
95
        $e = $this->getPrevious();
96
97
        if ($e instanceof RequestException && $e->getResponse() !== null) {
98
            return $e->getResponse();
99
        }
100
101
        return null;
102
    }
103
}
104