Completed
Pull Request — master (#90)
by Arnaud
03:11
created

ListFormHandler::handle()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 48
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 48
ccs 0
cts 22
cp 0
rs 8.7396
cc 4
eloc 28
nc 4
nop 2
crap 20
1
<?php
2
3
namespace LAG\AdminBundle\Form\Handler;
4
5
use LAG\AdminBundle\Admin\AdminInterface;
6
use LAG\AdminBundle\Form\Type\MassActionType;
7
use Symfony\Component\Form\FormFactoryInterface;
8
use Symfony\Component\Form\FormInterface;
9
use Symfony\Component\PropertyAccess\PropertyAccess;
10
use Twig_Environment;
11
12
class ListFormHandler
13
{
14
    /**
15
     * @var FormFactoryInterface
16
     */
17
    private $formFactory;
18
    /**
19
     * @var Twig_Environment
20
     */
21
    private $twig;
22
    
23
    public function __construct(FormFactoryInterface $formFactory, Twig_Environment $twig)
24
    {
25
        $this->formFactory = $formFactory;
26
        $this->twig = $twig;
27
    }
28
    
29
    public function handle(FormInterface $form, AdminInterface $admin)
30
    {
31
        if (!$form->isValid()) {
32
            return;
33
        }
34
        $data = $form->getData();
35
    
36
        if (!array_key_exists('id', $data)) {
37
            return;
38
        }
39
        $entityClass = $admin
40
            ->getConfiguration()
41
            ->getParameter('entity')
42
        ;
43
        $entityProperty = $form
44
            ->getConfig()
45
            ->getOption('entity_property')
46
        ;
47
        $entityValues = [];
48
        $accessor = PropertyAccess::createPropertyAccessor();
49
    
50
        foreach ($data as $entity) {
51
            $entityValues[] = $accessor->getValue($entity, $entityProperty);
52
        }
53
        
54
        $massDeleteForm = $this
55
            ->formFactory
56
            ->create(MassActionType::class, [
57
                'entity_class' => $entityClass,
58
                'entity_property' => $entityProperty,
59
                'entity_values' => $entityValues,
60
            ])
61
        ;
62
    
63
        return $this
64
            ->twig
65
            ->render('@LAGAdmin/List/delete.html.twig', [
66
                'form' => $massDeleteForm->createView(),
67
        
68
            ])
69
        ;
70
        
71
        
72
        var_dump($data);
0 ignored issues
show
Unused Code introduced by
var_dump($data); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
Security Debugging Code introduced by
var_dump($data); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
73
        die;
0 ignored issues
show
Coding Style Compatibility introduced by
The method handle() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
74
    
75
    
76
    }
77
    
78
    
79
    
80
//    use EntityLabelTrait;
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
81
//
82
//    /**
83
//     * Return entities ids checked by user
84
//     *
85
//     * @param FormInterface $form
86
//     * @return array
87
//     */
88
//    public function handle(FormInterface $form)
89
//    {
90
//        $data = $form->getData();
91
//        $batchItems = [];
92
//        $cleanData = [
93
//            'ids' => [],
94
//            'batch_action' => $data['batch_action'],
95
//            'labels' => $this->getLabels($data['entities'])
96
//        ];
97
//
98
//        // find batch items checkbox values
99
//        foreach ($data as $name => $batchItem) {
100
//            if (substr($name, 0, 6) == 'batch_') {
101
//                $batchItems[$name] = $batchItem;
102
//            }
103
//        }
104
//        // check if they exists in entities displayed and if checkbox is checked
105
//        foreach ($batchItems as $name => $batchItem) {
106
//            $batchId = (int) str_replace('batch_', '', $name);
107
//
108
//            if (array_key_exists($batchId, $cleanData['labels']) && $batchItem === true) {
109
//                $cleanData['ids'][] = $batchId;
110
//            }
111
//        }
112
//        return $cleanData;
113
//    }
114
//
115
//    /**
116
//     * @param $entities
117
//     * @return array
118
//     */
119
//    protected function getLabels($entities)
120
//    {
121
//        $labels = [];
122
//
123
//        foreach ($entities as $entity) {
124
//            $labels[$entity->getId()] = $this->getEntityLabel($entity);
125
//        }
126
//        return $labels;
127
//    }
128
}
129