Passed
Pull Request — master (#18)
by Vladislav
07:44
created

CurlResponseHandler::bindDto()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
<?php
2
namespace Carpenstar\ByBitAPI\Core\Response;
3
4
use Carpenstar\ByBitAPI\Core\Enums\EnumOutputMode;
5
use Carpenstar\ByBitAPI\Core\Exceptions\ApiException;
6
use Carpenstar\ByBitAPI\Core\Builders\ResponseDtoBuilder;
7
use Carpenstar\ByBitAPI\Core\Interfaces\ICurlResponseDtoInterface;
8
use Carpenstar\ByBitAPI\Core\Interfaces\IResponseHandlerInterface;
9
use Carpenstar\ByBitAPI\Core\Objects\Collection\ArrayCollection;
10
use Carpenstar\ByBitAPI\Core\Objects\Collection\EntityCollection;
11
use Carpenstar\ByBitAPI\Core\Objects\Collection\StringCollection;
12
13
class CurlResponseHandler implements IResponseHandlerInterface
14
{
15
    /**
16
     * @var string $entityClassName
17
     */
18
    private string $entity;
19
20
    /**
21
     * @param string $className
22
     * @return void
23
     */
24
    public function bindDto(string $className): self
25
    {
26
        $this->entity = $className;
27
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Carpenstar\ByBitAPI\Core...nse\CurlResponseHandler which is incompatible with the documented return type void.
Loading history...
28
    }
29
30
    /**
31
     * @return IResponseHandlerInterface
32
     * @throws \Exception
33
     */
34
    public function handle(string $apiData, int $mode = EnumOutputMode::MODE_ARRAY): ICurlResponseDtoInterface
35
    {
36
        $data = json_decode($apiData, true);
37
38
        // If return NULL, then service blocked that ip, try again after 10 minutes
39
        if (is_null($data)) {
40
            $data = [
41
                'time' => time(),
42
                'retCode' => 400,
43
                'retMsg' => 'Forbidden, try again later',
44
                'result' => ['list' =>[]],
45
                'retExtInfo' => []
46
            ];
47
        }
48
49
        if (!empty($data["retCode"]) && (int)$data["retCode"] > 0) {
50
            throw new ApiException($data["retMsg"], $data["retCode"]);
51
        }
52
53
        $time = $data['time'] ?? 0;
54
        $code = $data['retCode'] ?? 0;
55
        $msg = $data['retMsg'] ?? '';
56
        $extMsg = $data['retExtInfo'] ?? [];
57
        $cursor = $data['result']['nextPageCursor'] ?? '';
58
59
60
        if (isset($data['result'][$this->entity::$rootDataKey])) {
0 ignored issues
show
Bug introduced by
The property rootDataKey does not exist on string.
Loading history...
61
            $data = $data['result'][$this->entity::$rootDataKey];
62
        } elseif (!empty($data['result'])) {
63
            $data = [$data['result']];
64
        } elseif (empty($data['result'])) {
65
            $data = [];
66
        }
67
68
        switch ($mode) {
69
            case EnumOutputMode::MODE_JSON:
70
                $collection = new StringCollection();
71
                $collection->push($apiData);
72
                break;
73
            case EnumOutputMode::MODE_ARRAY:
74
                $collection = new ArrayCollection();
75
                array_walk($data, function ($item) use ($collection) {
76
                    $collection->push($item);
77
                });
78
                break;
79
            case EnumOutputMode::MODE_ENTITY:
80
            default:
81
                $collection = new EntityCollection();
82
                array_walk($data, function ($item) use ($collection) {
83
                    if (!empty($item)) {
84
                        $collection->push(ResponseDtoBuilder::make($this->entity, $item));
85
                    }
86
                });
87
                break;
88
        }
89
90
        return (new CurlResponseDto())
0 ignored issues
show
Bug Best Practice introduced by
The expression return new Carpenstar\By...)->setBody($collection) returns the type Carpenstar\ByBitAPI\Core\Response\CurlResponseDto which is incompatible with the documented return type Carpenstar\ByBitAPI\Core...esponseHandlerInterface.
Loading history...
91
            ->setTime($time)
92
            ->setReturnCode($code)
93
            ->setReturnMessage($msg)
94
            ->setReturnExtendedInfo($extMsg)
95
            ->setNextPageCursor($cursor)
96
            ->setBody($collection);
97
    }
98
}