Completed
Branch master (01610a)
by Arnaud
06:29
created

ListFormHandler::handle()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 26
ccs 0
cts 22
cp 0
rs 8.439
cc 6
eloc 15
nc 9
nop 1
crap 42
1
<?php
2
3
namespace LAG\AdminBundle\Form\Handler;
4
5
use BlueBear\BaseBundle\Entity\Behaviors\Id;
6
use LAG\AdminBundle\Admin\Behaviors\EntityLabelTrait;
7
use Symfony\Component\Form\FormInterface;
8
9
class ListFormHandler
10
{
11
    use EntityLabelTrait;
12
13
    /**
14
     * Return entities ids checked by user
15
     *
16
     * @param FormInterface $form
17
     * @return array
18
     */
19
    public function handle(FormInterface $form)
20
    {
21
        $data = $form->getData();
22
        $batchItems = [];
23
        $cleanData = [
24
            'ids' => [],
25
            'batch_action' => $data['batch_action'],
26
            'labels' => $this->getLabels($data['entities'])
27
        ];
28
29
        // find batch items checkbox values
30
        foreach ($data as $name => $batchItem) {
31
            if (substr($name, 0, 6) == 'batch_') {
32
                $batchItems[$name] = $batchItem;
33
            }
34
        }
35
        // check if they exists in entities displayed and if checkbox is checked
36
        foreach ($batchItems as $name => $batchItem) {
37
            $batchId = (int) str_replace('batch_', '', $name);
38
39
            if (array_key_exists($batchId, $cleanData['labels']) && $batchItem === true) {
40
                $cleanData['ids'][] = $batchId;
41
            }
42
        }
43
        return $cleanData;
44
    }
45
46
    /**
47
     * @param $entities
48
     * @return array
49
     */
50
    protected function getLabels($entities)
51
    {
52
        $labels = [];
53
        /** @var Id $entity */
54
        foreach ($entities as $entity) {
55
            $labels[$entity->getId()] = $this->getEntityLabel($entity);
56
        }
57
        return $labels;
58
    }
59
}
60