StackPlugin   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 4
dl 0
loc 56
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A doHandleRequest() 0 26 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Http\HttplugBundle\Collector;
6
7
use Exception;
8
use Http\Client\Common\Plugin;
9
use Psr\Http\Message\RequestInterface;
10
use Psr\Http\Message\ResponseInterface;
11
12
/**
13
 * The StackPlugin must be used as first Plugin in a client stack. It's used to detect when a new request start by
14
 * creating a new Stack and pushing it to the Collector.
15
 *
16
 * @author Fabien Bourigault <[email protected]>
17
 *
18
 * @internal
19
 */
20
class StackPlugin implements Plugin
21
{
22
    use Plugin\VersionBridgePlugin;
23
24
    /**
25
     * @var Collector
26
     */
27
    private $collector;
28
29
    /**
30
     * @var string
31
     */
32
    private $client;
33
34
    /**
35
     * @var Formatter
36
     */
37
    private $formatter;
38
39
    /**
40
     * @param string $client
41
     */
42 13
    public function __construct(Collector $collector, Formatter $formatter, $client)
43
    {
44 13
        $this->collector = $collector;
45 13
        $this->formatter = $formatter;
46 13
        $this->client = $client;
47 13
    }
48
49 9
    protected function doHandleRequest(RequestInterface $request, callable $next, callable $first)
0 ignored issues
show
Unused Code introduced by
The parameter $first is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
50
    {
51 9
        $stack = new Stack($this->client, $this->formatter->formatRequest($request));
52
53 9
        $this->collector->addStack($stack);
54 9
        $this->collector->activateStack($stack);
55
56
        $onFulfilled = function (ResponseInterface $response) use ($stack) {
57 5
            $stack->setResponse($this->formatter->formatResponse($response));
58
59 5
            return $response;
60 9
        };
61
62
        $onRejected = function (Exception $exception) use ($stack) {
63 1
            $stack->setResponse($this->formatter->formatException($exception));
64 1
            $stack->setFailed(true);
65
66 1
            throw $exception;
67 9
        };
68
69
        try {
70 9
            return $next($request)->then($onFulfilled, $onRejected);
71
        } finally {
72 9
            $this->collector->deactivateStack($stack);
73
        }
74
    }
75
}
76