Completed
Pull Request — master (#73)
by Tobias
05:50 queued 03:31
created

ResponseConverter::convert()   C

Complexity

Conditions 10
Paths 7

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 22
rs 6.1369
cc 10
eloc 17
nc 7
nop 3

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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