DiactorosMessageFactory::createRequest()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 13
c 1
b 0
f 0
nc 2
nop 5
dl 0
loc 22
ccs 8
cts 8
cp 1
crap 2
rs 9.8333
1
<?php
2
3
namespace Http\Message\MessageFactory;
4
5
use Http\Message\MessageFactory;
6
use Http\Message\StreamFactory\DiactorosStreamFactory;
7
use Laminas\Diactoros\Request as LaminasRequest;
8
use Laminas\Diactoros\Response as LaminasResponse;
9
use Zend\Diactoros\Request as ZendRequest;
0 ignored issues
show
Bug introduced by
The type Zend\Diactoros\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Zend\Diactoros\Response as ZendResponse;
0 ignored issues
show
Bug introduced by
The type Zend\Diactoros\Response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
12
/**
13
 * Creates Diactoros messages.
14
 *
15
 * @author GeLo <[email protected]>
16
 *
17
 * @deprecated This will be removed in php-http/message2.0. Consider using the official Diactoros PSR-17 factory
18
 */
19
final class DiactorosMessageFactory implements MessageFactory
20
{
21
    /**
22 4
     * @var DiactorosStreamFactory
23
     */
24 4
    private $streamFactory;
25 4
26
    public function __construct()
27
    {
28
        $this->streamFactory = new DiactorosStreamFactory();
0 ignored issues
show
Deprecated Code introduced by
The class Http\Message\StreamFactory\DiactorosStreamFactory has been deprecated: This will be removed in php-http/message2.0. Consider using the official Diactoros PSR-17 factory ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

28
        $this->streamFactory = /** @scrutinizer ignore-deprecated */ new DiactorosStreamFactory();
Loading history...
29
    }
30 1
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function createRequest(
35
        $method,
36
        $uri,
37 1
        array $headers = [],
38 1
        $body = null,
39 1
        $protocolVersion = '1.1'
40 1
    ) {
41 1
        if (class_exists(LaminasRequest::class)) {
42 1
            return (new LaminasRequest(
43
                $uri,
44
                $method,
45
                $this->streamFactory->createStream($body),
46
                $headers
47
            ))->withProtocolVersion($protocolVersion);
48 1
        }
49
50
        return (new ZendRequest(
51
            $uri,
52
            $method,
53
            $this->streamFactory->createStream($body),
54
            $headers
55 1
        ))->withProtocolVersion($protocolVersion);
56 1
    }
57 1
58 1
    /**
59 1
     * {@inheritdoc}
60
     */
61
    public function createResponse(
62
        $statusCode = 200,
63
        $reasonPhrase = null,
64
        array $headers = [],
65
        $body = null,
66
        $protocolVersion = '1.1'
67
    ) {
68
        if (class_exists(LaminasResponse::class)) {
69
            return (new LaminasResponse(
70
                $this->streamFactory->createStream($body),
71
                $statusCode,
72
                $headers
73
            ))->withProtocolVersion($protocolVersion);
74
        }
75
76
        return (new ZendResponse(
77
            $this->streamFactory->createStream($body),
78
            $statusCode,
79
            $headers
80
        ))->withProtocolVersion($protocolVersion);
81
    }
82
}
83