ContentLengthPlugin   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 3
dl 0
loc 23
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A handleRequest() 0 17 3
1
<?php
2
3
namespace Http\Client\Plugin;
4
5 1
@trigger_error('The '.__NAMESPACE__.'\ContentLengthPlugin class is deprecated since version 1.1 and will be removed in 2.0. Use Http\Client\Common\Plugin\ContentLengthPlugin instead.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
6
7
use Http\Message\Encoding\ChunkStream;
8
use Psr\Http\Message\RequestInterface;
9
10
/**
11
 * Allow to set the correct content length header on the request or to transfer it as a chunk if not possible.
12
 *
13
 * @author Joel Wurtz <[email protected]>
14
 *
15
 * @deprecated since since version 1.1, and will be removed in 2.0. Use {@link \Http\Client\Common\Plugin\ContentLengthPlugin} instead.
16
 */
17
class ContentLengthPlugin implements Plugin
0 ignored issues
show
Deprecated Code introduced by
The interface Http\Client\Plugin\Plugin has been deprecated with message: since since version 1.1, and will be removed in 2.0. Use {@link \Http\Client\Common\Plugin} instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22 2
    public function handleRequest(RequestInterface $request, callable $next, callable $first)
23
    {
24 2
        if (!$request->hasHeader('Content-Length')) {
25 2
            $stream = $request->getBody();
26
27
            // Cannot determine the size so we use a chunk stream
28 2
            if (null === $stream->getSize()) {
29 1
                $stream = new ChunkStream($stream);
30 1
                $request = $request->withBody($stream);
31 1
                $request = $request->withAddedHeader('Transfer-Encoding', 'chunked');
32 1
            } else {
33 1
                $request = $request->withHeader('Content-Length', $stream->getSize());
34
            }
35 2
        }
36
37 2
        return $next($request);
38
    }
39
}
40