Passed
Pull Request — master (#87)
by Dmitriy
14:25 queued 11:27
created

ApiResponseDataFactory::getErrorMessage()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 4
cts 5
cp 0.8
crap 3.072
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Infrastructure\Http\Response;
6
7
use Yiisoft\DataResponse\DataResponse;
8
use Yiisoft\Http\Status;
9
10
final class ApiResponseDataFactory
11
{
12 13
    public function createFromResponse(DataResponse $response): ApiResponse
13
    {
14 13
        if ($response->getStatusCode() !== Status::OK) {
15
            return $this
16 6
                ->createErrorResponse()
17 6
                ->setErrorCode($response->getStatusCode())
18 6
                ->setErrorMessage($this->getErrorMessage($response));
19
        }
20
21
        return $this
22 7
            ->createSuccessResponse()
23 7
            ->setData($response->getData());
0 ignored issues
show
Bug introduced by
It seems like $response->getData() can also be of type object; however, parameter $data of App\Infrastructure\Http\...\ApiResponse::setData() does only seem to accept array|null, maybe add an additional type check? ( Ignorable by Annotation )

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

23
            ->setData(/** @scrutinizer ignore-type */ $response->getData());
Loading history...
24
    }
25
26 7
    public function createSuccessResponse(): ApiResponse
27
    {
28
        return $this
29 7
            ->createResponse()
30 7
            ->setStatus('success');
31
    }
32
33 6
    public function createErrorResponse(): ApiResponse
34
    {
35
        return $this
36 6
            ->createResponse()
37 6
            ->setStatus('failed');
38
    }
39
40 13
    public function createResponse(): ApiResponse
41
    {
42 13
        return new ApiResponse();
43
    }
44
45 6
    private function getErrorMessage(DataResponse $response): string
46
    {
47 6
        $data = $response->getData();
48 6
        if (is_string($data) && !empty($data)) {
49 6
            return $data;
50
        }
51
52
        return 'Unknown error';
53
    }
54
}
55