Passed
Push — develop ( 5f199e...56f9a8 )
by Mathieu
02:20
created

FormReader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 21
dl 0
loc 74
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A configureCreateFields() 0 9 1
A loadDefaultFields() 0 19 3
A configureEditFields() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Neimheadh\SonataAnnotationBundle\Reader;
6
7
use Doctrine\Common\Annotations\Reader;
8
use Doctrine\ORM\Mapping\GeneratedValue;
9
use Neimheadh\SonataAnnotationBundle\Annotation\FormField;
10
use ReflectionClass;
11
use Sonata\AdminBundle\Form\FormMapper;
12
13
/**
14
 * Form configuration reader.
15
 *
16
 * @author Marko Kunic <[email protected]>
17
 * @author Mathieu Wambre <[email protected]>
18
 */
19
final class FormReader extends AbstractFieldConfigurationReader
20
{
21
22
    /**
23
     * {@inheritDoc}
24
     */
25
    public function __construct(
26
        Reader $annotationReader
27
    ) {
28
        parent::__construct($annotationReader, FormField::class);
29
    }
30
31
    /**
32
     * Build creation fields configuration.
33
     *
34
     * @param ReflectionClass $class      Entity class.
35
     * @param FormMapper      $formMapper Admin form mapper.
36
     *
37
     * @return void
38
     */
39
    public function configureCreateFields(
40
        ReflectionClass $class,
41
        FormMapper $formMapper
42
    ): void {
43
        $this->configureReaderFields(
44
            $class,
45
            $formMapper,
46
            $this->annotationClass,
47
            FormField::ACTION_CREATE
48
        );
49
    }
50
51
    /**
52
     * Build edit fields configuration.
53
     *
54
     * @param ReflectionClass $class      Entity class.
55
     * @param FormMapper      $formMapper Admin form mapper.
56
     *
57
     * @return void
58
     */
59
    public function configureEditFields(
60
        ReflectionClass $class,
61
        FormMapper $formMapper
62
    ): void {
63
        $this->configureReaderFields(
64
            $class,
65
            $formMapper,
66
            $this->annotationClass,
67
            FormField::ACTION_EDIT
68
        );
69
    }
70
71
    /**
72
     * {@inheritDoc}
73
     */
74
    protected function loadDefaultFields(
75
        ReflectionClass $class,
76
        string $annotationClass
77
    ): array {
78
        $properties = [];
79
80
        foreach ($class->getProperties() as $property) {
81
            if (!$this->annotationReader->getPropertyAnnotation(
82
                $property,
83
                GeneratedValue::class
84
            )) {
85
                $properties[] = [
86
                    'name' => $property->getName(),
87
                    'annotation' => new $annotationClass(),
88
                ];
89
            }
90
        }
91
92
        return $properties;
93
    }
94
95
}
96