Completed
Push — master ( b65b29...f39982 )
by Tobias
03:16 queued 01:01
created

src/Http/ResponseConverter.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Happyr\LinkedIn\Http;
4
5
use Happyr\LinkedIn\Exception\InvalidArgumentException;
6
use Happyr\LinkedIn\Exception\LinkedInTransferException;
7
use Psr\Http\Message\ResponseInterface;
8
9
class ResponseConverter
10
{
11
    /**
12
     * Convert a PSR-7 response to a data type you want to work with.
13
     *
14
     * @param ResponseInterface $response
15
     * @param string            $format
0 ignored issues
show
There is no parameter named $format. Did you maybe mean $requestFormat?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
16
     * @param string            $dataType
17
     *
18
     * @return ResponseInterface|\Psr\Http\Message\StreamInterface|\SimpleXMLElement|string
19
     *
20
     * @throws InvalidArgumentException
21
     * @throws LinkedInTransferException
22
     */
23
    public static function convert(ResponseInterface $response, $requestFormat, $dataType)
24
    {
25
        if (($requestFormat === 'json' && $dataType === 'simple_xml') ||
26
            ($requestFormat === 'xml' && $dataType === 'array')) {
27
            throw new InvalidArgumentException('Can not use reponse data format "%s" with the request format "%s".', $dataType, $requestFormat);
28
        }
29
30
        switch ($dataType) {
31
            case 'array':
32
                return self::convertToArray($response);
33
            case 'string':
34
                return $response->getBody()->__toString();
35
            case 'simple_xml':
36
                return self::convertToSimpleXml($response);
37
            case 'stream':
38
                return $response->getBody();
39
            case 'psr7':
40
                return $response;
41
            default:
42
                throw new InvalidArgumentException('Format "%s" is not supported', $dataType);
43
        }
44
    }
45
46
    /**
47
     * @param ResponseInterface $response
48
     *
49
     * @return string
50
     */
51
    public static function convertToArray(ResponseInterface $response)
52
    {
53
        return json_decode($response->getBody(), true);
54
    }
55
56
    /**
57
     * @param ResponseInterface $response
58
     *
59
     * @return \SimpleXMLElement
60
     *
61
     * @throws LinkedInTransferException
62
     */
63
    public static function convertToSimpleXml(ResponseInterface $response)
64
    {
65
        $body = $response->getBody();
66
        try {
67
            return new \SimpleXMLElement((string) $body ?: '<root />');
68
        } catch (\Exception $e) {
69
            throw new LinkedInTransferException('Unable to parse response body into XML.');
70
        }
71
    }
72
}
73