Test Failed
Push — master ( 9e6cf9...d006e3 )
by Petr
04:08
created

RestController::setLocation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 2
crap 2
1
<?php
2
3
namespace AppBundle\Controller\Infrastructure;
4
5
use AppBundle\Entity\Infrasctucture\AbstractRepository;
6
use AppBundle\Response\CollectionApiResponse;
7
use AppBundle\Response\FileResponse;
8
use AppBundle\Response\Infrastructure\AbstractApiResponse;
9
use AppBundle\Response\Infrastructure\HttpLocationInterface;
10
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
11
use Symfony\Component\Form\FormBuilder;
12
use Symfony\Component\Form\FormError;
13
use Symfony\Component\Form\FormInterface;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
17
/**
18
 * @author Vehsamrak
19
 */
20
class RestController extends Controller
21
{
22
23
    const MIME_TYPE_JSON = 'application/json';
24 24
25
    public function respond(AbstractApiResponse $apiResponse): Response
26 24
    {
27
        $response = $this->get('rockparade.response_factory')->createResponse($apiResponse);
28 24
29 24
        $this->setLocation($response, $apiResponse);
30
        $this->setContentType($apiResponse, $response);
31 24
32
        return $response;
33
    }
34
35
    /**
36
     * Process form
37
     * @param Request $request
38
     * @param FormInterface $form
39 10
     */
40
    protected function processForm(Request $request, FormInterface $form)
41 10
    {
42 10
        $formData = json_decode($request->getContent(), true) ?? $request->request->all();
43
        $clearMissing = $request->getMethod() != Request::METHOD_PATCH;
44 10
45 10
        $form->submit($formData, $clearMissing);
46
    }
47
48
    /**
49
     * @return string[]
50 4
     */
51
    protected function getFormErrors(FormInterface $form): array
52
    {
53 4
        /** @var string[] $errors */
54
        $errors = [];
55
56 4
        /** @var FormError $error */
57 4
        foreach ($form->getErrors(true) as $error) {
58 4
            $parametersString = join(',', $error->getMessageParameters());
1 ignored issue
show
Bug introduced by
The method getMessageParameters() does not seem to exist on object<Symfony\Component\Form\FormErrorIterator>.

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...
59 4
            if (!$parametersString || $parametersString === 'null') {
60
                $errors[] = $error->getMessage();
1 ignored issue
show
Bug introduced by
The method getMessage() does not seem to exist on object<Symfony\Component\Form\FormErrorIterator>.

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...
61 4
            } else {
62
                $errors[] = sprintf('%s - %s', $parametersString, $error->getMessage());
1 ignored issue
show
Bug introduced by
The method getMessage() does not seem to exist on object<Symfony\Component\Form\FormErrorIterator>.

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...
63
            }
64
        }
65 4
66
        return $errors;
67
    }
68
69 10
    /** {@inheritDoc} */
70
    protected function createFormBuilder($data = null, array $options = []): FormBuilder
71 10
    {
72
        $options['allow_extra_fields'] = true;
73 10
74
        return parent::createFormBuilder($data, $options);
75
    }
76
77
    /**
78
     * Create collection api response with total, limit and offset parameters
79
     * @param integer|null $limit
80
     * @param integer|null $offset
81 3
     */
82
    protected function createCollectionResponse(AbstractRepository $repository, $limit, $offset): CollectionApiResponse
83 3
    {
84 3
        $limit = (int) filter_var($limit, FILTER_VALIDATE_INT);
85
        $offset = (int) filter_var($offset, FILTER_VALIDATE_INT);
86 3
87 3
        $entities = $repository->findAllWithLimitAndOffset($limit, $offset);
88
        $entitiesQuantity = $repository->countAll();
89 3
90
        $response = new CollectionApiResponse($entities, Response::HTTP_OK, $entitiesQuantity, $limit, $offset);
91 3
92
        return $response;
93
    }
94 24
95
    private function setLocation(Response $response, AbstractApiResponse $apiResponse)
96 24
    {
97 2
        if ($apiResponse instanceof HttpLocationInterface) {
98
            $response->headers->set('Location', $apiResponse->getLocation());
99 24
        }
100
    }
101 24
102
    private function setContentType(AbstractApiResponse $apiResponse, Response $response)
103 24
    {
104 24
        if (!$apiResponse instanceof FileResponse) {
105
            $response->headers->set('Content-Type', self::MIME_TYPE_JSON);
106
        }
107
    }
108
}
109