ProfilePlugin::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 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 ProfilePlugin decorates any Plugin to fill Profile to keep representation of plugin input/output. Created profile
14
 * is pushed in the current Stack.
15
 *
16
 * @author Fabien Bourigault <[email protected]>
17
 *
18
 * @internal
19
 */
20
class ProfilePlugin implements Plugin
21
{
22
    use Plugin\VersionBridgePlugin;
23
24
    /**
25
     * @var Plugin
26
     */
27
    private $plugin;
28
29
    /**
30
     * @var Collector
31
     */
32
    private $collector;
33
34
    /**
35
     * @var Formatter
36
     */
37
    private $formatter;
38
39 13
    public function __construct(Plugin $plugin, Collector $collector, Formatter $formatter)
40
    {
41 13
        $this->plugin = $plugin;
42 13
        $this->collector = $collector;
43 13
        $this->formatter = $formatter;
44 13
    }
45
46 9
    protected function doHandleRequest(RequestInterface $request, callable $next, callable $first)
47
    {
48 9
        $profile = new Profile(get_class($this->plugin));
49
50 9
        $stack = $this->collector->getActiveStack();
51 9
        $stack->addProfile($profile);
52
53
        // wrap the next callback to profile the plugin request changes
54
        $wrappedNext = function (RequestInterface $request) use ($next, $profile) {
55 8
            $this->onOutgoingRequest($request, $profile);
56
57 8
            return $next($request);
58 9
        };
59
60
        // wrap the first callback to profile the plugin request changes
61
        $wrappedFirst = function (RequestInterface $request) use ($first, $profile) {
62
            $this->onOutgoingRequest($request, $profile);
63
64
            return $first($request);
65 9
        };
66
67
        try {
68 9
            $promise = $this->plugin->handleRequest($request, $wrappedNext, $wrappedFirst);
69 1
        } catch (Exception $e) {
70 1
            $this->onException($request, $profile, $e, $stack);
71
72 1
            throw $e;
73
        }
74
75
        return $promise->then(function (ResponseInterface $response) use ($profile, $request, $stack) {
76 7
            $this->onOutgoingResponse($response, $profile, $request, $stack);
77
78 7
            return $response;
79
        }, function (Exception $exception) use ($profile, $request, $stack) {
80 1
            $this->onException($request, $profile, $exception, $stack);
81
82 1
            throw $exception;
83 8
        });
84
    }
85
86
    /**
87
     * @param Stack $stack
88
     */
89 2
    private function onException(
90
        RequestInterface $request,
91
        Profile $profile,
92
        Exception $exception,
93
        Stack $stack = null
94
    ) {
95 2
        $profile->setFailed(true);
96 2
        $profile->setResponse($this->formatter->formatException($exception));
97 2
        $this->collectRequestInformation($request, $stack);
98 2
    }
99
100 8
    private function onOutgoingRequest(RequestInterface $request, Profile $profile)
101
    {
102 8
        $profile->setRequest($this->formatter->formatRequest($request));
103 8
    }
104
105
    /**
106
     * @param Stack $stack
107
     */
108 7
    private function onOutgoingResponse(ResponseInterface $response, Profile $profile, RequestInterface $request, Stack $stack = null)
109
    {
110 7
        $profile->setResponse($this->formatter->formatResponse($response));
111 7
        $this->collectRequestInformation($request, $stack);
112 7
    }
113
114
    /**
115
     * Collect request information when not already done by the HTTP client. This happens when using the CachePlugin
116
     * and the cache is hit without re-validation.
117
     */
118 9
    private function collectRequestInformation(RequestInterface $request, Stack $stack = null)
119
    {
120 9
        if (empty($stack->getRequestTarget())) {
121 7
            $stack->setRequestTarget($request->getRequestTarget());
0 ignored issues
show
Bug introduced by
It seems like $stack is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
122
        }
123 9
        if (empty($stack->getRequestMethod())) {
124 7
            $stack->setRequestMethod($request->getMethod());
125
        }
126 9
        if (empty($stack->getRequestScheme())) {
127 7
            $stack->setRequestScheme($request->getUri()->getScheme());
128
        }
129 9
        if (empty($stack->getRequestHost())) {
130 7
            $stack->setRequestHost($request->getUri()->getHost());
131
        }
132 9
        if (empty($stack->getClientRequest())) {
133 7
            $stack->setClientRequest($this->formatter->formatRequest($request));
134
        }
135 9
        if (empty($stack->getCurlCommand())) {
136 7
            $stack->setCurlCommand($this->formatter->formatAsCurlCommand($request));
137
        }
138 9
    }
139
}
140