RestController::createFormBuilder()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
ccs 3
cts 3
cp 1
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace AppBundle\Controller\Infrastructure;
4
5
use AppBundle\Entity\Infrasctucture\AbstractRepository;
6
use AppBundle\Enum\ApiOperation;
7
use AppBundle\Exception\EntityNotFoundException;
8
use AppBundle\Response\ApiError;
9
use AppBundle\Response\ApiResponse;
10
use AppBundle\Response\CollectionApiResponse;
11
use AppBundle\Response\FileResponse;
12
use AppBundle\Response\Infrastructure\AbstractApiResponse;
13
use AppBundle\Response\Infrastructure\HttpLocationInterface;
14
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
15
use Symfony\Component\Form\FormBuilder;
16
use Symfony\Component\Form\FormInterface;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
20
/**
21
 * @author Vehsamrak
22
 */
23
class RestController extends Controller
24
{
25
26
    const MIME_TYPE_JSON = 'application/json';
27
28 44
    public function respond(AbstractApiResponse $apiResponse): Response
29
    {
30 44
        $response = $this->get('rockparade.http_response_factory')->createResponse($apiResponse);
31
32 44
        $this->setLocation($response, $apiResponse);
33 44
        $this->setContentType($apiResponse, $response);
34
35 44
        return $response;
36
    }
37
38 9
    protected function createAndProcessForm(
39
        Request $request,
40
        string $type,
41
        $data = null,
42
        $options = []
43
    ) {
44 9
        $form = parent::createForm($type, $data, $options);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (createForm() instead of createAndProcessForm()). Are you sure this is correct? If so, you might want to change this to $this->createForm().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
45 9
        $this->processForm($request, $form);
46
47 9
        return $form;
48
    }
49
50
    /**
51
     * Process form
52
     * @param Request $request
53
     * @param FormInterface $form
54
     */
55 19
    protected function processForm(Request $request, FormInterface $form)
56
    {
57 19
        $formData = json_decode($request->getContent(), true) ?? $request->request->all();
58 19
        $clearMissing = $request->getMethod() != Request::METHOD_PATCH;
59
60 19
        $form->submit($formData, $clearMissing);
61 19
    }
62
63
    /** {@inheritDoc} */
64 4
    protected function createFormBuilder($data = null, array $options = []): FormBuilder
65
    {
66 4
        $options['allow_extra_fields'] = true;
67
68 4
        return parent::createFormBuilder($data, $options);
69
    }
70
71
    /**
72
     * Create collection api response with total, limit and offset parameters
73
     * @param integer|null $limit
74
     * @param integer|null $offset
75
     */
76 4
    protected function listEntities(AbstractRepository $repository, $limit, $offset): Response
77
    {
78 4
        $limit = (int) filter_var($limit, FILTER_VALIDATE_INT);
79 4
        $offset = (int) filter_var($offset, FILTER_VALIDATE_INT);
80
81 4
        $entities = $repository->findAllWithLimitAndOffset($limit, $offset);
82 4
        $entitiesQuantity = $repository->countAll();
83
84 4
        $response = new CollectionApiResponse($entities, Response::HTTP_OK, $entitiesQuantity, $limit, $offset);
85
86 4
        return $this->respond($response);
87
    }
88
89
    /**
90
     * @param string $entityFullName Entity class name
91
     * @param string|int $id Entity id
92
     * @return ApiError
93
     * @throws EntityNotFoundException
94
     */
95 4
    protected function createEntityNotFoundResponse(string $entityFullName, $id): ApiError
96
    {
97 4
        return $this->get('rockparade.entity_service')->createEntityNotFoundResponse($entityFullName, $id);
98
    }
99
100
    /**
101
     * @param string|int $id
102
     */
103 18
    protected function viewEntity(AbstractRepository $repository, $id): Response
104
    {
105 18
        $entity = $repository->findOneById($id);
106 18
        $entityClass = $repository->getClassName();
107
108 18
        if ($entity) {
109 14
            $response = new ApiResponse($entity, Response::HTTP_OK);
110
        } else {
111 4
            $response = $this->createEntityNotFoundResponse($entityClass, $id);
112
        }
113
114 18
        return $this->respond($response);
115
    }
116
117 44
    private function setLocation(Response $response, AbstractApiResponse $apiResponse)
118
    {
119 44
        if ($apiResponse instanceof HttpLocationInterface) {
120 6
            $response->headers->set('Location', $apiResponse->getLocation());
121
        }
122 44
    }
123
124 44
    private function setContentType(AbstractApiResponse $apiResponse, Response $response)
125
    {
126 44
        if (!$apiResponse instanceof FileResponse) {
127 44
            $response->headers->set('Content-Type', self::MIME_TYPE_JSON);
128
        }
129 44
    }
130
131
    /**
132
     * Create ApiOperation from Request method
133
     */
134 9
    protected function createApiOperation(Request $request): ApiOperation
135
    {
136 9
        return new ApiOperation($request->getMethod());
137
    }
138
}
139