Passed
Pull Request — master (#73)
by Dmitriy
22:17 queued 07:13
created

DeferredResponseFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
eloc 7
c 2
b 0
f 0
dl 0
loc 22
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A createResponseInternal() 0 8 2
A createResponse() 0 3 1
1
<?php
2
3
namespace App;
4
5
use Nyholm\Psr7\Response;
6
use Psr\Http\Message\ResponseFactoryInterface;
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\StreamFactoryInterface;
9
10
class DeferredResponseFactory implements ResponseFactoryInterface
11
{
12
    private StreamFactoryInterface $streamFactory;
13
14
    public function __construct(StreamFactoryInterface $streamFactory)
15
    {
16
        $this->streamFactory = $streamFactory;
17
    }
18
19
    public function createResponse(int $code = 200, string $reasonPhrase = '', $data = null): ResponseInterface
20
    {
21
        return new DeferredResponse($data, $this->createResponseInternal(), $this->streamFactory);
22
    }
23
24
    private function createResponseInternal(int $code = 200, string $reasonPhrase = ''): ResponseInterface
25
    {
26
        if (2 > \func_num_args()) {
27
            // This will make the Response class to use a custom reasonPhrase
28
            $reasonPhrase = null;
29
        }
30
31
        return new Response($code, [], null, '1.1', $reasonPhrase);
32
    }
33
}
34