Passed
Branch feature/refactoring (13cbf0)
by Alexey
03:46
created

GooglePlayException   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 53.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 12
c 1
b 0
f 0
dl 0
loc 63
ccs 8
cts 15
cp 0.5333
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A setUrl() 0 5 1
A getResponse() 0 9 3
A getUrl() 0 3 1
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