Passed
Pull Request — master (#52)
by Daniel
05:34
created

UploadableAction   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
eloc 14
c 0
b 0
f 0
dl 0
loc 23
ccs 0
cts 13
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 21 4
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Component Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentBundle\Action\Uploadable;
15
16
use Silverback\ApiComponentBundle\AnnotationReader\UploadableAnnotationReader;
17
use Silverback\ApiComponentBundle\Exception\InvalidArgumentException;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
20
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
21
22
/**
23
 * @author Daniel West <[email protected]>
24
 */
25
class UploadableAction
26
{
27
    public function __invoke(Request $request, UploadableAnnotationReader $uploadableHelper)
28
    {
29
        $contentType = $request->headers->get('CONTENT_TYPE');
30
        if (null === $contentType) {
1 ignored issue
show
introduced by
The condition null === $contentType is always false.
Loading history...
31
            throw new UnsupportedMediaTypeHttpException('The "Content-Type" header must exist.');
32
        }
33
34
        $formats = ['multipart/form-data'];
35
        if (\in_array(strtolower($contentType), $formats, true)) {
36
            throw new UnsupportedMediaTypeHttpException(sprintf('The content-type "%s" is not supported. Supported MIME type is "%s".', $contentType, implode('", "', $formats)));
37
        }
38
39
        $resourceClass = $request->attributes->get('_api_resource_class');
40
        $resource = new $resourceClass();
41
        try {
42
            $uploadableHelper->setUploadedFile($resource, $request->files);
0 ignored issues
show
Bug introduced by
The method setUploadedFile() does not exist on Silverback\ApiComponentB...oadableAnnotationReader. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
            $uploadableHelper->/** @scrutinizer ignore-call */ 
43
                               setUploadedFile($resource, $request->files);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
43
        } catch (InvalidArgumentException $exception) {
44
            throw new BadRequestHttpException($exception->getMessage());
45
        }
46
47
        return $resource;
48
    }
49
}
50