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

FilterEvent::clearFilters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
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