ResponseCastable   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 32
dl 0
loc 66
ccs 30
cts 30
cp 1
rs 10
c 3
b 1
f 0
wmc 13

2 Methods

Rating   Name   Duplication   Size   Complexity  
A castResponseToType() 0 20 6
B detectAndCastResponseToType() 0 24 7
1
<?php
2
3
/*
4
 * This file is part of the overtrue/wechat.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace EasyWeChat\Kernel\Traits;
13
14
use EasyWeChat\Kernel\Contracts\Arrayable;
15
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
16
use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
17
use EasyWeChat\Kernel\Http\Response;
18
use EasyWeChat\Kernel\Support\Collection;
19
use Psr\Http\Message\ResponseInterface;
20
21
/**
22
 * Trait ResponseCastable.
23
 *
24
 * @author overtrue <[email protected]>
25
 */
26
trait ResponseCastable
27
{
28
    /**
29
     * @param \Psr\Http\Message\ResponseInterface $response
30
     * @param string|null                         $type
31
     *
32
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
33
     *
34
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
35
     */
36 24
    protected function castResponseToType(ResponseInterface $response, $type = null)
37
    {
38 24
        $response = Response::buildFromPsrResponse($response);
39 24
        $response->getBody()->rewind();
40
41 24
        switch ($type ?? 'array') {
42 24
            case 'collection':
43 3
                return $response->toCollection();
44 23
            case 'array':
45 23
                return $response->toArray();
46 2
            case 'object':
47 1
                return $response->toObject();
48 2
            case 'raw':
49 2
                return $response;
50
            default:
51 1
                if (!is_subclass_of($type, Arrayable::class)) {
52 1
                    throw new InvalidConfigException(sprintf('Config key "response_type" classname must be an instanceof %s', Arrayable::class));
53
                }
54
55 1
                return new $type($response);
56
        }
57
    }
58
59
    /**
60
     * @param mixed       $response
61
     * @param string|null $type
62
     *
63
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
64
     *
65
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
66
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
67
     */
68 8
    protected function detectAndCastResponseToType($response, $type = null)
69
    {
70
        switch (true) {
71 8
            case $response instanceof ResponseInterface:
72 1
                $response = Response::buildFromPsrResponse($response);
73
74 1
                break;
75 8
            case $response instanceof Arrayable:
76 1
                $response = new Response(200, [], json_encode($response->toArray()));
77
78 1
                break;
79 8
            case ($response instanceof Collection) || is_array($response) || is_object($response):
80 8
                $response = new Response(200, [], json_encode($response));
81
82 8
                break;
83 1
            case is_scalar($response):
84 1
                $response = new Response(200, [], (string) $response);
85
86 1
                break;
87
            default:
88 1
                throw new InvalidArgumentException(sprintf('Unsupported response type "%s"', gettype($response)));
89
        }
90
91 8
        return $this->castResponseToType($response, $type);
92
    }
93
}
94