Passed
Push — master ( 641929...562012 )
by Alexey
06:04 queued 12s
created

GooglePlayException   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 36.84%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 16
c 1
b 0
f 0
dl 0
loc 81
ccs 7
cts 19
cp 0.3684
rs 10

5 Methods

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