Passed
Push — master ( a0632e...2294fc )
by Peter
03:03
created

Form   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 65
c 1
b 0
f 0
dl 0
loc 177
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A addIdentifier() 0 11 1
A addToName() 0 8 1
A addFailureUrl() 0 15 1
A addToEmail() 0 8 1
A addSuccessUrl() 0 15 1
A create() 0 22 2
A addMaxBodyLength() 0 20 1
A __construct() 0 3 1
A addName() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Contact\Form\Factory;
6
7
use AbterPhp\Contact\Domain\Entities\Form as Entity;
8
use AbterPhp\Framework\Constant\Html5;
9
use AbterPhp\Framework\Form\Container\FormGroup;
10
use AbterPhp\Framework\Form\Element\Input;
11
use AbterPhp\Framework\Form\Extra\Help;
12
use AbterPhp\Framework\Form\Factory\Base;
13
use AbterPhp\Framework\Form\Factory\IFormFactory;
14
use AbterPhp\Framework\Form\IForm;
15
use AbterPhp\Framework\Form\Label\Label;
16
use AbterPhp\Framework\I18n\ITranslator;
17
use Opulence\Orm\IEntity;
18
use Opulence\Sessions\ISession;
19
20
class Form extends Base
21
{
22
    /**
23
     * Form constructor.
24
     *
25
     * @param ISession    $session
26
     * @param ITranslator $translator
27
     */
28
    public function __construct(ISession $session, ITranslator $translator)
29
    {
30
        parent::__construct($session, $translator);
31
    }
32
33
    /**
34
     * @param string       $action
35
     * @param string       $method
36
     * @param string       $showUrl
37
     * @param IEntity|null $entity
38
     *
39
     * @return IForm
40
     */
41
    public function create(string $action, string $method, string $showUrl, ?IEntity $entity = null): IForm
42
    {
43
        if (!($entity instanceof Entity)) {
44
            throw new \InvalidArgumentException(IFormFactory::ERR_MSG_ENTITY_MISSING);
45
        }
46
47
        $this->createForm($action, $method)
48
            ->addDefaultElements()
49
            ->addName($entity)
50
            ->addIdentifier($entity)
51
            ->addToName($entity)
52
            ->addToEmail($entity)
53
            ->addSuccessUrl($entity)
54
            ->addFailureUrl($entity)
55
            ->addMaxBodyLength($entity)
56
            ->addDefaultButtons($showUrl);
57
58
        $form = $this->form;
59
60
        $this->form = null;
61
62
        return $form;
63
    }
64
65
    /**
66
     * @param Entity $entity
67
     *
68
     * @return $this
69
     */
70
    protected function addName(Entity $entity): Form
71
    {
72
        $input = new Input('name', 'name', $entity->getName());
73
        $label = new Label('name', 'contact:formName');
74
75
        $this->form[] = new FormGroup($input, $label);
76
77
        return $this;
78
    }
79
80
    /**
81
     * @param Entity $entity
82
     *
83
     * @return $this
84
     */
85
    protected function addIdentifier(Entity $entity): Form
86
    {
87
        $this->form[] = new Input(
88
            'identifier',
89
            'identifier',
90
            $entity->getIdentifier(),
91
            [],
92
            [Html5::ATTR_TYPE => Input::TYPE_HIDDEN]
93
        );
94
95
        return $this;
96
    }
97
98
    /**
99
     * @param Entity $entity
100
     *
101
     * @return $this
102
     */
103
    protected function addToName(Entity $entity): Form
104
    {
105
        $input = new Input('to_name', 'to_name', $entity->getToName());
106
        $label = new Label('to_name', 'contact:formToName');
107
108
        $this->form[] = new FormGroup($input, $label);
109
110
        return $this;
111
    }
112
113
    /**
114
     * @param Entity $entity
115
     *
116
     * @return $this
117
     */
118
    protected function addToEmail(Entity $entity): Form
119
    {
120
        $input = new Input('to_email', 'to_email', $entity->getToEmail(), [], [Html5::ATTR_TYPE => Input::TYPE_EMAIL]);
121
        $label = new Label('to_email', 'contact:formToEmail');
122
123
        $this->form[] = new FormGroup($input, $label);
124
125
        return $this;
126
    }
127
128
    /**
129
     * @param Entity $entity
130
     *
131
     * @return $this
132
     */
133
    protected function addSuccessUrl(Entity $entity): Form
134
    {
135
        $input = new Input(
136
            'success_url',
137
            'success_url',
138
            $entity->getSuccessUrl(),
139
            [],
140
            [Html5::ATTR_TYPE => Input::TYPE_URL]
141
        );
142
        $label = new Label('success_url', 'contact:formSuccessUrl');
143
        $help  = new Help('contact:hintSuccessUrl');
144
145
        $this->form[] = new FormGroup($input, $label, $help);
146
147
        return $this;
148
    }
149
150
    /**
151
     * @param Entity $entity
152
     *
153
     * @return $this
154
     */
155
    protected function addFailureUrl(Entity $entity): Form
156
    {
157
        $input = new Input(
158
            'failure_url',
159
            'failure_url',
160
            $entity->getFailureUrl(),
161
            [],
162
            [Html5::ATTR_TYPE => Input::TYPE_URL]
163
        );
164
        $label = new Label('failure_url', 'contact:formFailureUrl');
165
        $help  = new Help('contact:hintFailureUrl');
166
167
        $this->form[] = new FormGroup($input, $label, $help);
168
169
        return $this;
170
    }
171
172
    /**
173
     * @param Entity $entity
174
     *
175
     * @return $this
176
     */
177
    protected function addMaxBodyLength(Entity $entity): Form
178
    {
179
        $input = new Input(
180
            'max_body_length',
181
            'max_body_length',
182
            (string)$entity->getMaxBodyLength(),
183
            [],
184
            [
185
                Html5::ATTR_TYPE => Input::TYPE_NUMBER,
186
                Html5::ATTR_MIN  => 0,
187
                Html5::ATTR_MAX  => 10000,
188
                Html5::ATTR_STEP => 100,
189
            ]
190
        );
191
        $label = new Label('max_body_length', 'contact:formMaxBodyLength');
192
        $help  = new Help('contact:hintMaxBodyLength');
193
194
        $this->form[] = new FormGroup($input, $label, $help);
195
196
        return $this;
197
    }
198
}
199