Completed
Pull Request — master (#9)
by Iakov
03:57
created

BuildCreateFormStep::execute()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 0
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Kami\ApiCoreBundle\RequestProcessor\Step\Create;
4
5
use Doctrine\Common\Annotations\Reader;
6
use Kami\ApiCoreBundle\Annotation\Form;
7
use Kami\ApiCoreBundle\RequestProcessor\Step\AbstractBuildFormStep;
8
use Kami\ApiCoreBundle\RequestProcessor\Step\Common\GetEntityFromReflectionStep;
9
use Kami\ApiCoreBundle\RequestProcessor\Step\Common\GetReflectionFromRequestStep;
10
use Kami\ApiCoreBundle\RequestProcessor\Step\Common\ValidateResourceAccessStep;
11
use Kami\ApiCoreBundle\Security\AccessManager;
12
use Symfony\Component\Form\FormFactoryInterface;
13
14
15
class BuildCreateFormStep extends AbstractBuildFormStep
16
{
17
    public function execute()
18
    {
19
        $builder = $this->getBaseFormBuilder();
20
        /** @var \ReflectionClass $reflection */
21
        $reflection = $this->getFromResponse('reflection');
22
23
        foreach ($reflection->getProperties() as $property) {
24
            if ($this->accessManager->canCreateProperty($property)) {
25
                if ($annotation = $this->reader->getPropertyAnnotation($property, Form::class)) {
26
                    $builder->add($property->getName(), $annotation->type, $annotation->options);
27
                }
28
                $builder->add($property->getName());
29
            }
30
        }
31
32
        return $this->createResponse(['form' => $builder->getForm()]);
33
    }
34
35
36
    public function requiresBefore()
37
    {
38
        return [
39
            ValidateResourceAccessStep::class,
40
            GetEntityFromReflectionStep::class,
41
            GetReflectionFromRequestStep::class
42
        ];
43
    }
44
}