Completed
Push — dev ( 5504ea...330909 )
by Arnaud
02:52
created

BatchFormHandler::handle()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 37
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 37
ccs 0
cts 29
cp 0
rs 8.439
cc 6
eloc 21
nc 9
nop 1
crap 42
1
<?php
2
3
namespace LAG\AdminBundle\Form\Handler;
4
5
use LAG\AdminBundle\Admin\Behaviors\EntityLabel;
6
use LAG\AdminBundle\Form\Type\BatchActionType;
7
use Symfony\Component\Form\FormFactory;
8
use Symfony\Component\Form\FormInterface;
9
10
class BatchFormHandler
11
{
12
    use EntityLabel;
13
14
    /**
15
     * @var FormFactory
16
     */
17
    protected $formFactory;
18
19
    public function __construct(FormFactory $formBuilder)
20
    {
21
        $this->formFactory = $formBuilder;
22
    }
23
24
    /**
25
     * Return entities checked by user
26
     *
27
     * @param FormInterface $form
28
     * @return FormInterface
29
     */
30
    public function handle(FormInterface $form)
31
    {
32
        $data = $form->getData();
33
        // TODO sort entities by id ?
34
        $entities = $data['entities'];
35
        $bacthItems = [];
36
        $batchEntities = [];
37
38
        // find batch items checkbox values
39
        foreach ($data as $name => $batchItem) {
40
            if (substr($name, 0, 6) == 'batch_') {
41
                $bacthItems[$name] = $batchItem;
42
            }
43
        }
44
        // check if they exists in entities displayed and if checkbox is checked
45
        foreach ($bacthItems as $name => $bacthItem) {
46
            $batchId = (int)str_replace('batch_', '', $name);
47
48
            if (array_key_exists($batchId, $entities) && $bacthItem === true) {
49
                $batchEntities[$batchId] = $this->getEntityLabel($entities[$batchId]);
50
            }
51
        }
52
        // TODO get action from configuration
53
        $action = 'lag.admin.batch.delete';
54
55
        $form = $this
56
            ->formFactory
57
            ->create(new BatchActionType(), [
0 ignored issues
show
Documentation introduced by
new \LAG\AdminBundle\Form\Type\BatchActionType() is of type object<LAG\AdminBundle\Form\Type\BatchActionType>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
58
                'entities_ids' => array_keys($batchEntities),
59
                'entities_labels' => $batchEntities,
60
                'batch_action' => $data['batch']
61
            ], [
62
                'action' => $action
63
            ]);
64
65
        return $form;
66
    }
67
}
68