Passed
Pull Request — master (#165)
by Arnaud
37:57
created

FilterEvent   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 33.33%

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 66
ccs 4
cts 12
cp 0.3333
rs 10
c 0
b 0
f 0
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A addForm() 0 6 2
A removeForm() 0 6 2
A clearFilters() 0 3 1
A addFilter() 0 3 1
A getFilters() 0 3 1
A getForms() 0 3 1
A hasForm() 0 3 1
1
<?php
2
3
namespace LAG\AdminBundle\Event\Events;
4
5
use LAG\AdminBundle\Event\AbstractEvent;
6
use LAG\AdminBundle\Exception\Exception;
7
use LAG\AdminBundle\Filter\FilterInterface;
8
use Symfony\Component\Form\FormInterface;
9
10
class FilterEvent extends AbstractEvent
11
{
12
    /**
13
     * @var FilterInterface[]
14
     */
15
    private $filters = [];
16
17
    /**
18
     * @var FormInterface[]
19
     */
20
    private $forms = [];
21
22
    /**
23
     * @throws Exception
24
     */
25
    public function addForm(FormInterface $form, string $identifier): void
26
    {
27
        if ($this->hasForm($identifier)) {
28
            throw new Exception('A form with the identifier "'.$identifier.'" was already added. Use removeForm() before adding the new form');
29
        }
30
        $this->forms[$identifier] = $form;
31
    }
32
33
    public function removeForm(string $identifier): void
34
    {
35
        if (!$this->hasForm($identifier)) {
36 8
            throw new  Exception('The form "'.$identifier.'" does not exists');
37
        }
38 8
        unset($this->forms[$identifier]);
39
    }
40
41
    public function hasForm(string $identifier): bool
42
    {
43
        return array_key_exists($identifier, $this->forms);
44
    }
45
46
    /**
47
     * @return FormInterface[]
48
     */
49
    public function getForms()
50
    {
51
        return $this->forms;
52 8
    }
53
54 8
    /**
55
     * Add a filter and its value.
56
     */
57
    public function addFilter(FilterInterface $filter): void
58
    {
59
        $this->filters[] = $filter;
60
    }
61
62
    /**
63
     * Remove all added filters.
64
     */
65
    public function clearFilters(): void
66
    {
67
        $this->filters = [];
68
    }
69
70
    /**
71
     * @return FilterInterface[]
72
     */
73
    public function getFilters(): array
74
    {
75
        return $this->filters;
76
    }
77
}
78