Passed
Pull Request — master (#72)
by Dmitriy
10:17
created

JsonDataConverter   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 25
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A convertData() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App;
6
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\StreamFactoryInterface;
9
use Psr\Http\Message\StreamInterface;
10
use Yiisoft\Serializer\JsonSerializer;
11
12
final class JsonDataConverter implements DataConverterInterface
13
{
14
    /**
15
     * @var string the Content-Type header for the response
16
     */
17
    private string $contentType = 'application/json';
18
19
    private JsonSerializer $jsonSerializer;
20
21
    private StreamFactoryInterface $streamFactory;
22
23
    public function __construct(
24
        JsonSerializer $jsonSerializer,
25
        StreamFactoryInterface $streamFactory
26
    ) {
27
        $this->jsonSerializer = $jsonSerializer;
28
        $this->streamFactory = $streamFactory;
29
    }
30
31
    public function convertData($data, ResponseInterface &$response): StreamInterface
32
    {
33
        $content = $this->jsonSerializer->serialize($data);
34
        $response = $response->withHeader('Content-Type', $this->contentType);
35
36
        return $this->streamFactory->createStream($content);
37
    }
38
}
39