Completed
Pull Request — master (#73)
by Tobias
04:31 queued 02:17
created

ResponseConverter   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 0
cbo 3
dl 0
loc 67
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
C convert() 0 22 10
A convertToArray() 0 4 1
A convertToSimpleXml() 0 16 3
1
<?php
2
3
namespace Happyr\LinkedIn\Http;
4
5
use Happyr\LinkedIn\Exceptions\LinkedInApiException;
6
use Psr\Http\Message\ResponseInterface;
7
8
class ResponseConverter
9
{
10
    /**
11
     * @param ResponseInterface $response
12
     * @param string            $format
0 ignored issues
show
Documentation introduced by
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...
13
     *
14
     * @return ResponseInterface|\Psr\Http\Message\StreamInterface|\SimpleXMLElement|string
15
     *
16
     * @throws LinkedInApiException
17
     */
18
    public static function convert(ResponseInterface $response, $requestFormat, $dataType)
19
    {
20
        if (($requestFormat === 'json' && $dataType === 'simple_xml') ||
21
            ($requestFormat === 'xml' && $dataType === 'array')) {
22
            throw new \InvalidArgumentException(sprintf('Can not use reponse data format "%s" with the request format "s%"', $dataType, $requestFormat));
23
        }
24
25
        switch ($dataType) {
26
            case 'array':
27
                return self::convertToArray($response);
28
            case 'string':
29
                return $response->getBody()->__toString();
30
            case 'simple_xml':
31
                return self::convertToSimpleXml($response);
32
            case 'stream':
33
                return $response->getBody();
34
            case 'psr7':
35
                return $response;
36
            default:
37
                throw new \InvalidArgumentException(sprintf('Format "%s" is not supported', $dataType));
38
        }
39
    }
40
41
    /**
42
     * @param ResponseInterface $response
43
     *
44
     * @return string
45
     */
46
    public static function convertToArray(ResponseInterface $response)
47
    {
48
        return json_decode($response->getBody(), true);
49
    }
50
51
    /**
52
     * @param ResponseInterface $response
53
     *
54
     * @return \SimpleXMLElement
55
     *
56
     * @throws LinkedInApiException
57
     */
58
    public static function convertToSimpleXml(ResponseInterface $response)
59
    {
60
        $body = $response->getBody();
61
        try {
62
            return new \SimpleXMLElement((string) $body ?: '<root />');
63
        } catch (\Exception $e) {
64
            throw new LinkedInApiException(
65
                array(
66
                    'error' => array(
67
                        'message' => 'Unable to parse response body into XML: '.$e->getMessage(),
68
                        'type' => 'XmlParseException',
69
                    ),
70
                )
71
            );
72
        }
73
    }
74
}
75