| Conditions | 11 |
| Paths | 34 |
| Total Lines | 63 |
| Code Lines | 45 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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])) { |
||
| 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()) |
||
| 91 | ->setTime($time) |
||
| 92 | ->setReturnCode($code) |
||
| 93 | ->setReturnMessage($msg) |
||
| 94 | ->setReturnExtendedInfo($extMsg) |
||
| 95 | ->setNextPageCursor($cursor) |
||
| 96 | ->setBody($collection); |
||
| 97 | } |
||
| 98 | } |