Passed
Push — feature/uploadable ( 7c6d25...a7ed20 )
by Daniel
11:07
created

FormPostPatchAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 4
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
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\Form;
15
16
use Silverback\ApiComponentsBundle\Action\AbstractAction;
17
use Silverback\ApiComponentsBundle\Entity\Component\Form;
18
use Silverback\ApiComponentsBundle\Factory\Response\ResponseFactory;
19
use Silverback\ApiComponentsBundle\Form\Handler\FormSubmitHandler;
20
use Silverback\ApiComponentsBundle\Serializer\SerializeFormatResolver;
21
use Symfony\Component\HttpFoundation\Request;
22
use Symfony\Component\Serializer\SerializerInterface;
23
24
/**
25
 * @author Daniel West <[email protected]>
26
 */
27
class FormPostPatchAction extends AbstractAction
28
{
29
    private FormSubmitHandler $formSubmitHandler;
30
31
    public function __construct(SerializerInterface $serializer, SerializeFormatResolver $requestFormatResolver, ResponseFactory $responseFactory, FormSubmitHandler $formSubmitHandler)
32
    {
33
        parent::__construct($serializer, $requestFormatResolver, $responseFactory);
34
        $this->formSubmitHandler = $formSubmitHandler;
35
    }
36
37
    public function __invoke(Request $request, Form $data)
38
    {
39
        $decodedContent = $this->serializer->decode($request->getContent(), $this->requestFormatResolver->getFormatFromRequest($request), []);
40
        $isPatchRequest = Request::METHOD_PATCH === $request->getMethod();
41
        $response = $this->formSubmitHandler->handle($decodedContent, $isPatchRequest, $data, $this->requestFormatResolver->getFormatFromRequest($request));
42
43
        return $this->responseFactory->create($request, $response->getContent(), $response->getStatusCode());
44
    }
45
}
46