Completed
Pull Request — dev (#48)
by Arnaud
03:46
created

ListFormHandler   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

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 25
cts 25
cp 1
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 1
    public function handle(FormInterface $form)
20
    {
21 1
        $data = $form->getData();
22 1
        $batchItems = [];
23
        $cleanData = [
24 1
            'ids' => [],
25 1
            'batch_action' => $data['batch_action'],
26 1
            'labels' => $this->getLabels($data['entities'])
27 1
        ];
28
29
        // find batch items checkbox values
30 1
        foreach ($data as $name => $batchItem) {
31 1
            if (substr($name, 0, 6) == 'batch_') {
32 1
                $batchItems[$name] = $batchItem;
33 1
            }
34 1
        }
35
        // check if they exists in entities displayed and if checkbox is checked
36 1
        foreach ($batchItems as $name => $batchItem) {
37 1
            $batchId = (int) str_replace('batch_', '', $name);
38
39 1
            if (array_key_exists($batchId, $cleanData['labels']) && $batchItem === true) {
40 1
                $cleanData['ids'][] = $batchId;
41 1
            }
42 1
        }
43 1
        return $cleanData;
44
    }
45
46
    /**
47
     * @param $entities
48
     * @return array
49
     */
50 1
    protected function getLabels($entities)
51
    {
52 1
        $labels = [];
53
        /** @var Id $entity */
54 1
        foreach ($entities as $entity) {
55 1
            $labels[$entity->getId()] = $this->getEntityLabel($entity);
56 1
        }
57 1
        return $labels;
58
    }
59
}
60