|
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\ResponseInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Exception thrown if there is an error accessing the Google Play server. |
|
19
|
|
|
*/ |
|
20
|
|
|
class GooglePlayException extends \Exception |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var string|null URL associated with the exception. */ |
|
23
|
|
|
private $url; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Construct the GooglePlayException. |
|
27
|
|
|
* |
|
28
|
|
|
* @param string $message the Exception message to throw |
|
29
|
|
|
* @param int $code the Exception code |
|
30
|
|
|
* @param \Throwable $previous the previous throwable used for the exception chaining |
|
31
|
|
|
*/ |
|
32
|
9 |
|
public function __construct($message = '', $code = 0, ?\Throwable $previous = null) |
|
33
|
|
|
{ |
|
34
|
9 |
|
parent::__construct($message, $code, $previous); |
|
35
|
|
|
|
|
36
|
9 |
|
if ($previous instanceof RequestException) { |
|
37
|
6 |
|
$this->url = $previous->getRequest()->getUri()->__toString(); |
|
38
|
|
|
} |
|
39
|
9 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Set the URL associated with the exception. |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $url URL associated with the exception |
|
45
|
|
|
* |
|
46
|
|
|
* @return GooglePlayException returns the same object to support the call chain |
|
47
|
|
|
*/ |
|
48
|
2 |
|
public function setUrl(string $url): self |
|
49
|
|
|
{ |
|
50
|
2 |
|
$this->url = $url; |
|
51
|
|
|
|
|
52
|
2 |
|
return $this; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Returns the URL with which the exception is associated. |
|
57
|
|
|
* |
|
58
|
|
|
* URL maybe `null`. |
|
59
|
|
|
* |
|
60
|
|
|
* @return string|null URL associated with the exception or `null` |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getUrl(): ?string |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->url; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Returns an HTTP response if present. |
|
69
|
|
|
* |
|
70
|
|
|
* @return responseInterface|null PSR-7 ResponseInterface or null |
|
71
|
|
|
* |
|
72
|
|
|
* @see https://www.php-fig.org/psr/psr-7/ PSR-7: HTTP message interfaces |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getResponse(): ?ResponseInterface |
|
75
|
|
|
{ |
|
76
|
|
|
$e = $this->getPrevious(); |
|
77
|
|
|
|
|
78
|
|
|
if ($e instanceof RequestException && $e->getResponse() !== null) { |
|
79
|
|
|
return $e->getResponse(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return null; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|