Completed
Pull Request — master (#73)
by Tobias
04:16 queued 10s
created

ResponseConverter::convertToSimpleXml()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 9
rs 9.6667
cc 3
eloc 6
nc 2
nop 1
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
     * @param ResponseInterface $response
13
     * @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...
14
     *
15
     * @return ResponseInterface|\Psr\Http\Message\StreamInterface|\SimpleXMLElement|string
16
     *
17
     * @throws LinkedInTransferException
18
     */
19
    public static function convert(ResponseInterface $response, $requestFormat, $dataType)
20
    {
21
        if (($requestFormat === 'json' && $dataType === 'simple_xml') ||
22
            ($requestFormat === 'xml' && $dataType === 'array')) {
23
            throw new InvalidArgumentException('Can not use reponse data format "%s" with the request format "%s"', $dataType, $requestFormat);
24
        }
25
26
        switch ($dataType) {
27
            case 'array':
28
                return self::convertToArray($response);
29
            case 'string':
30
                return $response->getBody()->__toString();
31
            case 'simple_xml':
32
                return self::convertToSimpleXml($response);
33
            case 'stream':
34
                return $response->getBody();
35
            case 'psr7':
36
                return $response;
37
            default:
38
                throw new InvalidArgumentException('Format "%s" is not supported', $dataType);
39
        }
40
    }
41
42
    /**
43
     * @param ResponseInterface $response
44
     *
45
     * @return string
46
     */
47
    public static function convertToArray(ResponseInterface $response)
48
    {
49
        return json_decode($response->getBody(), true);
50
    }
51
52
    /**
53
     * @param ResponseInterface $response
54
     *
55
     * @return \SimpleXMLElement
56
     *
57
     * @throws LinkedInTransferException
58
     */
59
    public static function convertToSimpleXml(ResponseInterface $response)
60
    {
61
        $body = $response->getBody();
62
        try {
63
            return new \SimpleXMLElement((string) $body ?: '<root />');
64
        } catch (\Exception $e) {
65
            throw new LinkedInTransferException('Unable to parse response body into XML.');
66
        }
67
    }
68
}
69