Completed
Push — master ( 0eab77...b2f729 )
by Paul
05:35
created

buildForEntityAndWidgetType()   C

Complexity

Conditions 11
Paths 24

Size

Total Lines 50
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 50
rs 5.4893
c 0
b 0
f 0
cc 11
eloc 25
nc 24
nop 3

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Victoire\Bundle\CoreBundle\Form\Builder;
4
5
use Doctrine\ORM\EntityRepository;
6
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
7
use Symfony\Component\Translation\TranslatorInterface;
8
use Victoire\Bundle\BusinessEntityBundle\Entity\BusinessPropertyRepository;
9
use Victoire\Bundle\BusinessEntityBundle\Reader\BusinessEntityCacheReader;
10
11
/**
12
 * Edit Page Type.
13
 */
14
class EntityProxyFieldsBuilder
15
{
16
    private $translator;
17
    /**
18
     * @var EntityRepository
19
     */
20
    private $businessPropertyRepository;
21
    /**
22
     * @var EntityRepository
23
     */
24
    private $cacheReader;
25
26
    /**
27
     * define form fields.
28
     *
29
     * @param EntityRepository          $businessPropertyRepository
30
     * @param BusinessEntityCacheReader $cacheReader
31
     * @param TranslatorInterface       $translator
32
     */
33
    public function __construct(BusinessPropertyRepository $businessPropertyRepository, BusinessEntityCacheReader $cacheReader, TranslatorInterface $translator)
34
    {
35
        $this->businessPropertyRepository = $businessPropertyRepository;
36
        $this->cacheReader = $cacheReader;
0 ignored issues
show
Documentation Bug introduced by
It seems like $cacheReader of type object<Victoire\Bundle\B...inessEntityCacheReader> is incompatible with the declared type object<Doctrine\ORM\EntityRepository> of property $cacheReader.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
37
        $this->translator = $translator;
38
    }
39
40
    /**
41
     * Build.
42
     *
43
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
44
     * @param string                                       $widgetName
45
     * @param string                                       $namespace
0 ignored issues
show
Bug introduced by
There is no parameter named $namespace. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
46
     *
47
     * @throws \Exception
48
     *
49
     * @return array The all list of fields type to add for the entity namespace given
50
     */
51
    public function buildForEntityAndWidgetType(&$builder, $widgetName, $businessEntity)
52
    {
53
        //Try to add a new form for each entity with the correct annotation and business properties
54
        $rawBusinessProperties = $this->businessPropertyRepository->getByBusinessEntity($businessEntity)->run();
55
        $businessProperties = [];
56
        foreach ($rawBusinessProperties as $businessProperty) {
57
            foreach ($businessProperty->getTypes() as $type) {
58
                $businessProperties[$type][] = $businessProperty;
59
            }
60
        }
61
        $receiverPropertiesTypes = $this->cacheReader->getReceiverProperties($widgetName);
62
63
        if (!empty($receiverPropertiesTypes)) {
64
            foreach ($receiverPropertiesTypes as $type => $receiverProperties) {
65
                /* @var \Victoire\Bundle\BusinessEntityBundle\Entity\ReceiverProperty[] $receiverProperties */
66
                foreach ($receiverProperties as $receiverProperty) {
67
68
                    //Check if entity has all the required receiver properties as business properties
69
                    if (isset($businessProperties[$type]) && is_array($businessProperties[$type]) && count($businessProperties[$type])) {
70
71
                        //Create form types with field as key and values as choices
72
                        //TODO Add some formatter Class or a buildField method responsible to create this type
73
                        $label = $this->translator->trans(
74
                            'widget_'.strtolower($widgetName).'.form.'.$receiverProperty->getFieldName().'.label',
75
                            [],
76
                            'victoire'
77
                        );
78
                        $choices = [];
79
                        foreach ($businessProperties[$type] as $choice) {
80
                            $choices[$choice->getName()] = $choice->getName();
81
                        }
82
83
                        $options = [
84
                            'choices' => $choices,
85
                            'label'   => $label,
86
                            'attr'    => [
87
                                'title' => $label,
88
                            ],
89
                        ];
90
91
                        if (!$receiverProperty->isRequired()) {
92
                            $options = array_merge(['required' => false], $options);
93
                        }
94
95
                        $builder->add($receiverProperty->getFieldName(), ChoiceType::class, $options);
96
                    }
97
                }
98
            }
99
        }
100
    }
101
}
102