Completed
Push — master ( f6901d...949ee8 )
by David
03:53 queued 01:16
created

Deferred   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 120
Duplicated Lines 19.17 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 47.06%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 1
dl 23
loc 120
ccs 24
cts 51
cp 0.4706
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B then() 23 31 5
A getState() 0 4 1
A resolve() 0 13 3
A reject() 0 13 3
A wait() 0 17 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Http\Client\Common;
4
5
use Http\Client\Exception;
6
use Http\Promise\Promise;
7
use Psr\Http\Message\ResponseInterface;
8
9
/**
10
 * A deferred allow to return a promise which has not been resolved yet.
11
 */
12
class Deferred implements Promise
13
{
14
    private $value;
15
16
    private $failure;
17
18
    private $state;
19
20
    private $waitCallback;
21
22
    private $onFulfilledCallbacks;
23
24
    private $onRejectedCallbacks;
25
26 4
    public function __construct(callable $waitCallback)
27
    {
28 4
        $this->waitCallback = $waitCallback;
29 4
        $this->state = Promise::PENDING;
30 4
        $this->onFulfilledCallbacks = [];
31 4
        $this->onRejectedCallbacks = [];
32 4
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function then(callable $onFulfilled = null, callable $onRejected = null)
38
    {
39
        $deferred = new self($this->waitCallback);
40
41 View Code Duplication
        $this->onFulfilledCallbacks[] = function (ResponseInterface $response) use ($onFulfilled, $deferred) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
            try {
43
                if (null !== $onFulfilled) {
44
                    $response = $onFulfilled($response);
45
                }
46
                $deferred->resolve($response);
47
            } catch (Exception $exception) {
48
                $deferred->reject($exception);
49
            }
50
        };
51
52 View Code Duplication
        $this->onRejectedCallbacks[] = function (Exception $exception) use ($onRejected, $deferred) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
            try {
54
                if (null !== $onRejected) {
55
                    $response = $onRejected($exception);
56
                    $deferred->resolve($response);
57
58
                    return;
59
                }
60
                $deferred->reject($exception);
61
            } catch (Exception $newException) {
62
                $deferred->reject($newException);
63
            }
64
        };
65
66
        return $deferred;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function getState()
73
    {
74
        return $this->state;
75
    }
76
77
    /**
78
     * Resolve this deferred with a Response.
79
     */
80 3
    public function resolve(ResponseInterface $response)
81
    {
82 3
        if (self::PENDING !== $this->state) {
83
            return;
84
        }
85
86 3
        $this->value = $response;
87 3
        $this->state = self::FULFILLED;
88
89 3
        foreach ($this->onFulfilledCallbacks as $onFulfilledCallback) {
90
            $onFulfilledCallback($response);
91
        }
92 3
    }
93
94
    /**
95
     * Reject this deferred with an Exception.
96
     */
97 1
    public function reject(Exception $exception)
98
    {
99 1
        if (self::PENDING !== $this->state) {
100
            return;
101
        }
102
103 1
        $this->failure = $exception;
104 1
        $this->state = self::REJECTED;
105
106 1
        foreach ($this->onRejectedCallbacks as $onRejectedCallback) {
107
            $onRejectedCallback($exception);
108
        }
109 1
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114 4
    public function wait($unwrap = true)
115
    {
116 4
        if (self::PENDING === $this->state) {
117
            $callback = $this->waitCallback;
118
            $callback();
119
        }
120
121 4
        if (!$unwrap) {
122
            return;
123
        }
124
125 4
        if (self::FULFILLED === $this->state) {
126 3
            return $this->value;
127
        }
128
129 1
        throw $this->failure;
130
    }
131
}
132