Completed
Pull Request — master (#52)
by Tobias
03:52 queued 02:43
created

Promise   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 95.35%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 9
dl 0
loc 124
ccs 41
cts 43
cp 0.9535
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 25 4
A then() 0 4 1
A getState() 0 4 1
A wait() 0 12 3
A handleException() 0 26 5
1
<?php
2
3
namespace Http\Adapter\Guzzle6;
4
5
use GuzzleHttp\Exception as GuzzleExceptions;
6
use GuzzleHttp\Promise\PromiseInterface;
7
use Http\Client\Exception as HttplugException;
8
use Http\Promise\Promise as HttpPromise;
9
use Psr\Http\Message\RequestInterface;
10
use Psr\Http\Message\ResponseInterface;
11
12
/**
13
 * Wrapper around Guzzle promises.
14
 *
15
 * @author Joel Wurtz <[email protected]>
16
 */
17
class Promise implements HttpPromise
18
{
19
    /**
20
     * @var PromiseInterface
21
     */
22
    private $promise;
23
24
    /**
25
     * @var string State of the promise
26
     */
27
    private $state;
28
29
    /**
30
     * @var ResponseInterface
31
     */
32
    private $response;
33
34
    /**
35
     * @var HttplugException
36
     */
37
    private $exception;
38
39
    /**
40
     * @var RequestInterface
41
     */
42
    private $request;
43
44
    /**
45
     * @param PromiseInterface $promise
46
     * @param RequestInterface $request
47
     */
48 379
    public function __construct(PromiseInterface $promise, RequestInterface $request)
49
    {
50 379
        $this->request = $request;
51 379
        $this->state = self::PENDING;
52
        $this->promise = $promise->then(function ($response) {
53 367
            $this->response = $response;
54 367
            $this->state = self::FULFILLED;
55
56 367
            return $response;
57 379
        }, function ($reason) use ($request) {
58 11
            $this->state = self::REJECTED;
59
60 11
            if ($reason instanceof HttplugException) {
61 6
                $this->exception = $reason;
62 11
            } elseif ($reason instanceof GuzzleExceptions\GuzzleException) {
63 10
                $this->exception = $this->handleException($reason, $request);
64 1
            } elseif ($reason instanceof \Exception) {
65 1
                $this->exception = new \RuntimeException('Invalid exception returned from Guzzle6', 0, $reason);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \RuntimeException('I...m Guzzle6', 0, $reason) of type object<RuntimeException> is incompatible with the declared type object<Http\Client\Exception> of property $exception.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
66
            } else {
67
                $this->exception = new \UnexpectedValueException('Reason returned from Guzzle6 must be an Exception', 0, $reason);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \UnexpectedValueExce...Exception', 0, $reason) of type object<UnexpectedValueException> is incompatible with the declared type object<Http\Client\Exception> of property $exception.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
68
            }
69
70 11
            throw $this->exception;
71 379
        });
72 379
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 165
    public function then(callable $onFulfilled = null, callable $onRejected = null)
78
    {
79 165
        return new static($this->promise->then($onFulfilled, $onRejected), $this->request);
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 369
    public function getState()
86
    {
87 369
        return $this->state;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 378
    public function wait($unwrap = true)
94
    {
95 378
        $this->promise->wait(false);
96
97 378
        if ($unwrap) {
98 369
            if (self::REJECTED == $this->getState()) {
99 5
                throw $this->exception;
100
            }
101
102 364
            return $this->response;
103
        }
104 9
    }
105
106
    /**
107
     * Converts a Guzzle exception into an Httplug exception.
108
     *
109
     * @param GuzzleExceptions\GuzzleException $exception
110
     * @param RequestInterface                 $request
111
     *
112
     * @return HttplugException
113
     */
114 11
    private function handleException(GuzzleExceptions\GuzzleException $exception, RequestInterface $request)
115
    {
116 11
        if ($exception instanceof GuzzleExceptions\SeekException) {
117
            return new HttplugException\RequestException($exception->getMessage(), $request, $exception);
118
        }
119
120 11
        if ($exception instanceof GuzzleExceptions\ConnectException) {
121 11
            return new HttplugException\NetworkException($exception->getMessage(), $exception->getRequest(), $exception);
122
        }
123
124 1
        if ($exception instanceof GuzzleExceptions\RequestException) {
125
            // Make sure we have a response for the HttpException
126 1
            if ($exception->hasResponse()) {
127 1
                return new HttplugException\HttpException(
128 1
                    $exception->getMessage(),
129 1
                    $exception->getRequest(),
130 1
                    $exception->getResponse(),
0 ignored issues
show
Bug introduced by
It seems like $exception->getResponse() can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
131 1
                    $exception
132
                );
133
            }
134
135 1
            return new HttplugException\RequestException($exception->getMessage(), $exception->getRequest(), $exception);
136
        }
137
138 1
        return new HttplugException\TransferException($exception->getMessage(), 0, $exception);
139
    }
140
}
141