Passed
Pull Request — master (#572)
by Alexander
02:59 queued 01:28
created

ApiResponseDataFactory::createFromResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 12
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;
0 ignored issues
show
Bug introduced by
The type Yiisoft\DataResponse\DataResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Yiisoft\Http\Status;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Http\Status was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
10
final class ApiResponseDataFactory
11
{
12
    public function createFromResponse(DataResponse $response): ApiResponse
13
    {
14
        if ($response->getStatusCode() !== Status::OK) {
15
            return $this
16
                ->createErrorResponse()
17
                ->setErrorCode($response->getStatusCode())
18
                ->setErrorMessage($this->getErrorMessage($response));
19
        }
20
21
        return $this
22
            ->createSuccessResponse()
23
            ->setData($response->getData());
24
    }
25
26
    public function createSuccessResponse(): ApiResponse
27
    {
28
        return $this
29
            ->createResponse()
30
            ->setStatus('success');
31
    }
32
33
    public function createErrorResponse(): ApiResponse
34
    {
35
        return $this
36
            ->createResponse()
37
            ->setStatus('failed');
38
    }
39
40
    public function createResponse(): ApiResponse
41
    {
42
        return new ApiResponse();
43
    }
44
45
    private function getErrorMessage(DataResponse $response): string
46
    {
47
        $data = $response->getData();
48
        if (is_string($data) && !empty($data)) {
49
            return $data;
50
        }
51
52
        return 'Unknown error';
53
    }
54
}
55