ResponseCastable::castResponseToType()   B
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 17
c 1
b 0
f 0
nc 6
nop 2
dl 0
loc 23
rs 8.8333
1
<?php
2
3
namespace Freyo\ApiGateway\Kernel\Traits;
4
5
use Freyo\ApiGateway\Kernel\Exceptions\InvalidArgumentException;
6
use Freyo\ApiGateway\Kernel\Exceptions\InvalidConfigException;
7
use Freyo\ApiGateway\Kernel\Http\Response;
8
use Freyo\ApiGateway\Kernel\Contracts\Arrayable;
9
use Psr\Http\Message\ResponseInterface;
10
use Freyo\ApiGateway\Kernel\Support\Collection;
11
12
/**
13
 * Trait ResponseCastable.
14
 */
15
trait ResponseCastable
16
{
17
    /**
18
     * @param \Psr\Http\Message\ResponseInterface $response
19
     * @param string|null                         $type
20
     *
21
     * @return array|\Freyo\ApiGateway\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
22
     *
23
     * @throws \Freyo\ApiGateway\Kernel\Exceptions\InvalidConfigException
24
     */
25
    protected function castResponseToType(ResponseInterface $response, $type = null)
26
    {
27
        $response = Response::buildFromPsrResponse($response);
28
        $response->getBody()->rewind();
29
30
        switch ($type ?: 'array') {
31
            case 'collection':
32
                return $response->toCollection();
33
            case 'array':
34
                return $response->toArray();
35
            case 'object':
36
                return $response->toObject();
37
            case 'raw':
38
                return $response;
39
            default:
40
                if (!is_subclass_of($type, Arrayable::class)) {
41
                    throw new InvalidConfigException(sprintf(
42
                        'Config key "response_type" classname must be an instanceof %s',
43
                        Arrayable::class
44
                    ));
45
                }
46
47
                return new $type($response);
48
        }
49
    }
50
51
    /**
52
     * @param mixed       $response
53
     * @param string|null $type
54
     *
55
     * @return array|\Freyo\ApiGateway\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
56
     *
57
     * @throws \Freyo\ApiGateway\Kernel\Exceptions\InvalidArgumentException
58
     * @throws \Freyo\ApiGateway\Kernel\Exceptions\InvalidConfigException
59
     */
60
    protected function detectAndCastResponseToType($response, $type = null)
61
    {
62
        switch (true) {
63
            case $response instanceof ResponseInterface:
64
                $response = Response::buildFromPsrResponse($response);
65
66
                break;
67
            case ($response instanceof Collection) || is_array($response) || is_object($response):
68
                $response = new Response(200, [], json_encode($response));
69
70
                break;
71
            case is_scalar($response):
72
                $response = new Response(200, [], $response);
73
74
                break;
75
            default:
76
                throw new InvalidArgumentException(sprintf('Unsupported response type "%s"', gettype($response)));
77
        }
78
79
        return $this->castResponseToType($response, $type);
80
    }
81
}
82