Passed
Push — master ( 374b5e...c56e21 )
by Daniel
10:16
created

UploadAction::__invoke()   B

Complexity

Conditions 10
Paths 12

Size

Total Lines 49
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 28
c 1
b 0
f 0
nc 12
nop 5
dl 0
loc 49
ccs 0
cts 27
cp 0
crap 110
rs 7.6666

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
/*
4
 * This file is part of the Silverback API Components 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\ApiComponentsBundle\Action\Uploadable;
15
16
use Silverback\ApiComponentsBundle\Exception\InvalidArgumentException;
17
use Silverback\ApiComponentsBundle\Helper\Publishable\PublishableStatusChecker;
18
use Silverback\ApiComponentsBundle\Helper\Uploadable\UploadableFileManager;
19
use Silverback\ApiComponentsBundle\Serializer\Normalizer\PublishableNormalizer;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
22
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
23
use Symfony\Component\PropertyAccess\PropertyAccess;
24
25
/**
26
 * @author Daniel West <[email protected]>
27
 */
28
class UploadAction
29
{
30
    public function __invoke(?object $data, Request $request, UploadableFileManager $uploadableFileManager, PublishableStatusChecker $publishableStatusChecker, PublishableNormalizer $publishableNormalizer)
31
    {
32
        $contentType = $request->headers->get('CONTENT_TYPE');
33
        if (null === $contentType) {
1 ignored issue
show
introduced by
The condition null === $contentType is always false.
Loading history...
34
            throw new UnsupportedMediaTypeHttpException('The "Content-Type" header must exist.');
35
        }
36
37
        $contentType = explode(';', $contentType)[0];
38
        $formats = ['multipart/form-data'];
39
        if (!\in_array(strtolower($contentType), $formats, true)) {
40
            throw new UnsupportedMediaTypeHttpException(sprintf('The content-type "%s" is not supported. Supported MIME type is "%s".', $contentType, implode('", "', $formats)));
41
        }
42
43
        $resourceClass = $request->attributes->get('_api_resource_class');
44
        $resource = $data ?? new $resourceClass();
45
46
        /**
47
         * if it IS publishable
48
         * if NOT asking to update published ?published=true
49
         * if it IS currently published
50
         * if the user DOES have permission.
51
         */
52
        $publishableAnnotationReader = $publishableStatusChecker->getAnnotationReader();
53
        if ($publishableAnnotationReader->isConfigured($resource)) {
54
            $configuration = $publishableAnnotationReader->getConfiguration($resource);
55
            $isGranted = $publishableStatusChecker->isGranted($resource);
56
            if (!$data) {
57
                if (!$isGranted) {
58
                    $accessor = PropertyAccess::createPropertyAccessor();
59
                    $accessor->setValue($resource, $configuration->fieldName, date('Y-m-d H:i:s'));
60
                }
61
            } elseif (
62
                $isGranted &&
63
                !$publishableStatusChecker->isPublishedRequest($request) &&
64
                $publishableStatusChecker->isActivePublishedAt($resource)
65
            ) {
66
                $resource = $publishableNormalizer->createDraft($resource, $configuration, $resourceClass);
67
            }
68
        }
69
70
        try {
71
            $uploadableFileManager->setUploadedFilesFromFileBag($resource, $request->files);
72
        } catch (InvalidArgumentException $exception) {
73
            throw new UnprocessableEntityHttpException($exception->getMessage());
74
        }
75
76
        $request->attributes->set('data', $resource);
77
78
        return $resource;
79
    }
80
}
81