Passed
Pull Request — master (#24)
by Alexander
03:14
created

ApiResponseFormatter   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
dl 0
loc 20
ccs 8
cts 8
cp 1
rs 10
c 1
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A format() 0 7 1
A __construct() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Formatter;
6
7
use App\Factory\ApiResponseDataFactory;
8
use Psr\Http\Message\ResponseInterface;
9
use Yiisoft\DataResponse\DataResponse;
10
use Yiisoft\DataResponse\DataResponseFormatterInterface;
11
use Yiisoft\DataResponse\Formatter\JsonDataResponseFormatter;
12
13
final class ApiResponseFormatter implements DataResponseFormatterInterface
14
{
15
    private ApiResponseDataFactory $apiResponseDataFactory;
16
    private JsonDataResponseFormatter $jsonDataResponseFormatter;
17
18 11
    public function __construct(
19
        ApiResponseDataFactory $apiResponseDataFactory,
20
        JsonDataResponseFormatter $jsonDataResponseFormatter
21
    ) {
22 11
        $this->apiResponseDataFactory = $apiResponseDataFactory;
23 11
        $this->jsonDataResponseFormatter = $jsonDataResponseFormatter;
24 11
    }
25
26 11
    public function format(DataResponse $dataResponse): ResponseInterface
27
    {
28 11
        $response = $dataResponse->withData(
29 11
            $this->apiResponseDataFactory->createFromResponse($dataResponse)->toArray()
30
        );
31
32 11
        return $this->jsonDataResponseFormatter->format($response);
33
    }
34
}
35