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

JsonDataConverter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
rs 10
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
    private JsonSerializer $jsonSerializer;
15
    private StreamFactoryInterface $streamFactory;
16
17
    public function __construct(
18
        JsonSerializer $jsonSerializer,
19
        StreamFactoryInterface $streamFactory
20
    ) {
21
        $this->jsonSerializer = $jsonSerializer;
22
        $this->streamFactory = $streamFactory;
23
    }
24
25
    public function convertData($data, ResponseInterface &$response): StreamInterface
26
    {
27
        $content = $this->jsonSerializer->serialize($data);
28
        $response = $response->withHeader('Content-Type', $this->getContentType());
29
        return $this->streamFactory->createStream($content);
30
    }
31
32
    protected function getContentType(): string
33
    {
34
        return 'application/json';
35
    }
36
}
37