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

ListFormHandler   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 8
c 2
b 1
f 1
lcom 0
cbo 2
dl 0
loc 51
ccs 0
cts 30
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B handle() 0 26 6
A getLabels() 0 9 2
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