ContentToFormDataConverter::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 4
nop 1
1
<?php
2
3
namespace Saxulum\RestCrud\Request;
4
5
use Saxulum\RestCrud\Request\Converter\ConverterInterface;
6
use Symfony\Component\HttpFoundation\Request;
7
8
class ContentToFormDataConverter
9
{
10
    /**
11
     * @var ConverterInterface[]
12
     */
13
    protected $converters = [];
14
15
    /**
16
     * @param ConverterInterface[] $converters
17
     */
18
    public function __construct(array $converters)
19
    {
20
        foreach ($converters as $i => $converter) {
21
            if (!$converter instanceof ConverterInterface) {
22
                $type = is_object($converter) ? get_class($converter) : gettype($converter);
23
                throw new \InvalidArgumentException(
24
                    sprintf(
25
                        'Only objects implementing "%s" are supported! "%s" given on index %s',
26
                        'Saxulum\RestCrud\Request\Converter\ConverterInterface',
27
                        $type,
28
                        $i
29
                    )
30
                );
31
            }
32
            $this->converters[$converter->contentType()] = $converter;
33
        }
34
    }
35
36
    /**
37
     * @param Request $request
38
     *
39
     * @return Request
40
     */
41
    public function convert(Request &$request)
42
    {
43
        $contentType = $request->headers->get('Content-Type');
44
        if (false !== $pos = strpos($contentType, ';')) {
45
            $contentType = substr($contentType, 0, $pos);
46
        }
47
48
        if (in_array($contentType, array('application/x-www-form-urlencoded', 'multipart/form-data'), true)) {
49
            return $request;
50
        }
51
52
        $formData = $this->getConverter($contentType)->convert($request->getContent());
0 ignored issues
show
Bug introduced by
It seems like $request->getContent() targeting Symfony\Component\HttpFo...n\Request::getContent() can also be of type resource; however, Saxulum\RestCrud\Request...terInterface::convert() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $contentType defined by $request->headers->get('Content-Type') on line 43 can also be of type array<integer,string>; however, Saxulum\RestCrud\Request...nverter::getConverter() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
53
54
        $request = new Request(
55
            $request->query->all(),
56
            $formData,
57
            $request->attributes->all(),
58
            $request->cookies->all(),
59
            $request->files->all(),
60
            array_replace($request->server->all(), array(
61
                'CONTENT_TYPE' => 'application/x-www-form-urlencoded',
62
            )),
63
            http_build_query($formData)
64
        );
65
66
        return $request;
67
    }
68
69
    /**
70
     * @param string $contentType
71
     *
72
     * @return ConverterInterface
73
     *
74
     * @throws \Exception
75
     */
76
    protected function getConverter($contentType)
77
    {
78
        if (!isset($this->converters[$contentType])) {
79
            throw new \Exception(sprintf('There is no converter for content type: "%s', $contentType));
80
        }
81
82
        return $this->converters[$contentType];
83
    }
84
}
85