Completed
Push — master ( 6b8416...c520c0 )
by Filipe
02:12
created

HttpResponseException   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 1
dl 0
loc 49
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A request() 0 4 1
A response() 0 4 1
1
<?php
2
3
/**
4
 * This file is part of slick/http
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Http\Client\Exception;
11
12
use Psr\Http\Message\RequestInterface;
13
use Psr\Http\Message\ResponseInterface;
14
use RuntimeException;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Slick\Http\Client\Exception\RuntimeException.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
15
use Slick\Http\Exception;
16
use Throwable;
17
18
/**
19
 * Http Response Exception
20
 *
21
 * @package Slick\Http\Client\Exception
22
 */
23
abstract class HttpResponseException extends RuntimeException implements Exception
24
{
25
    /**
26
     * @var RequestInterface
27
     */
28
    private $request;
29
    /**
30
     * @var ResponseInterface
31
     */
32
    private $response;
33
34
    /**
35
     * Creates an Http Response Exception
36
     *
37
     * @param RequestInterface  $request
38
     * @param ResponseInterface $response
39
     * @param Throwable|null    $previous
40
     */
41
    public function __construct(
42
        RequestInterface $request,
43
        ResponseInterface $response,
44
        Throwable $previous = null
45
    )
46
    {
47
        parent::__construct($response->getReasonPhrase(), $response->getStatusCode(), $previous);
48
        $this->request = $request;
49
        $this->response = $response;
50
    }
51
52
    /**
53
     * The request made
54
     *
55
     * @return RequestInterface
56
     */
57
    public function request()
58
    {
59
        return $this->request;
60
    }
61
62
    /**
63
     * The response
64
     *
65
     * @return ResponseInterface
66
     */
67
    public function response()
68
    {
69
        return $this->response;
70
    }
71
}