AdminUserFilter::getInputFilter()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 96

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 96
rs 8.0872
c 0
b 0
f 0
cc 2
nc 2
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Admin\Filter;
6
7
use Zend\Db\Adapter\Adapter;
8
use Zend\InputFilter\InputFilter;
9
use Zend\InputFilter\InputFilterAwareInterface;
10
use Zend\InputFilter\InputFilterInterface;
11
12
class AdminUserFilter implements InputFilterAwareInterface
13
{
14
    protected $inputFilter;
15
    protected $adapter;
16
17
    public function __construct(Adapter $adapter)
18
    {
19
        $this->adapter = $adapter;
20
    }
21
22
    public function getInputFilter()
23
    {
24
        if (!$this->inputFilter) {
25
            $inputFilter = new InputFilter();
26
27
            $inputFilter->add(
28
                [
29
                    'name'       => 'first_name',
30
                    'required'   => true,
31
                    'filters'    => [['name' => 'StringTrim']],
32
                    'validators' => [
33
                        ['name' => 'NotEmpty'],
34
                        ['name' => 'StringLength', 'options' => ['min' => 2, 'max' => 255]],
35
                    ],
36
                ]
37
            );
38
39
            $inputFilter->add(
40
                [
41
                    'name'       => 'last_name',
42
                    'required'   => true,
43
                    'filters'    => [['name' => 'StringTrim']],
44
                    'validators' => [
45
                        ['name' => 'NotEmpty'],
46
                        ['name' => 'StringLength', 'options' => ['min' => 2, 'max' => 255]],
47
                    ],
48
                ]
49
            );
50
51
            $inputFilter->add(
52
                [
53
                    'name'       => 'email',
54
                    'required'   => true,
55
                    'filters'    => [['name' => 'StringTrim']],
56
                    'validators' => [
57
                        ['name' => 'NotEmpty'],
58
                        ['name' => 'EmailAddress'],
59
                        [
60
                            'name'    => 'dbnorecordexists',
61
                            'options' => ['adapter' => $this->adapter, 'table' => 'admin_users', 'field' => 'email'],
62
                        ],
63
                    ],
64
                ]
65
            );
66
67
            $inputFilter->add(
68
                [
69
                    'name'     => 'introduction',
70
                    'required' => false,
71
                    'filters'  => [['name' => 'StringTrim']],
72
                ]
73
            );
74
75
            $inputFilter->add(
76
                [
77
                    'name'     => 'biography',
78
                    'required' => false,
79
                    'filters'  => [['name' => 'StringTrim']],
80
                ]
81
            );
82
83
            $inputFilter->add(
84
                [
85
                    'name'       => 'password',
86
                    'required'   => true,
87
                    'validators' => [
88
                        ['name' => 'NotEmpty'],
89
                        ['name' => 'StringLength', 'options' => ['min' => 7, 'max' => 255]],
90
                    ],
91
                ]
92
            );
93
94
            $inputFilter->add(
95
                [
96
                    'name'       => 'confirm_password',
97
                    'required'   => true,
98
                    'validators' => [
99
                        ['name' => 'NotEmpty'],
100
                        ['name' => 'Identical', 'options' => ['token' => 'password']],
101
                    ],
102
                ]
103
            );
104
105
            $inputFilter->add(
106
                [
107
                    'name'       => 'status',
108
                    'required'   => true,
109
                    'validators' => [['name' => 'NotEmpty'], ['name' => 'Digits']],
110
                ]
111
            );
112
113
            $this->inputFilter = $inputFilter;
114
        }
115
116
        return $this->inputFilter;
117
    }
118
119
    public function setInputFilter(InputFilterInterface $inputFilter)
120
    {
121
        throw new \Exception('Not used');
122
    }
123
}
124