Passed
Push — master ( b47fe7...645f8c )
by Chris
29:23
created

FormFieldController   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 203
Duplicated Lines 0 %

Test Coverage

Coverage 96.88%

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 203
ccs 62
cts 64
cp 0.9688
rs 10
c 0
b 0
f 0
wmc 26

23 Methods

Rating   Name   Duplication   Size   Complexity  
A formatData() 0 3 1
A render() 0 3 1
A formatInput() 0 3 1
A processInput() 0 3 1
A getUpdatedValue() 0 3 1
A requestVarPresent() 0 3 1
A mustAwait() 0 3 1
A processData() 0 13 3
A getPresetValue() 0 3 1
A getRawInput() 0 3 1
A compose() 0 3 1
A normalizeFormField() 0 3 1
A getRequestVar() 0 3 1
A setFormFieldName() 0 5 1
A setMustAwait() 0 5 1
A getFormField() 0 3 1
A process() 0 6 1
A initCacheBuilder() 0 5 1
A isPermittedToProcess() 0 3 1
A setFormFieldValue() 0 5 1
A validate() 0 3 1
A inspect() 0 3 1
A __construct() 0 20 2
1
<?php
2
3
namespace WebTheory\Saveyour\Controller;
4
5
use Psr\Http\Message\ServerRequestInterface;
6
use WebTheory\Saveyour\Contracts\Controller\FormFieldControllerInterface;
7
use WebTheory\Saveyour\Contracts\Data\FieldDataManagerInterface;
8
use WebTheory\Saveyour\Contracts\Field\FormFieldInterface;
9
use WebTheory\Saveyour\Contracts\Formatting\DataFormatterInterface;
10
use WebTheory\Saveyour\Contracts\Report\Builder\ProcessedFieldReportBuilderInterface;
11
use WebTheory\Saveyour\Contracts\Report\ProcessedFieldReportInterface;
12
use WebTheory\Saveyour\Contracts\Report\ValidationReportInterface;
13
use WebTheory\Saveyour\Contracts\Validation\ValidatorInterface;
14
use WebTheory\Saveyour\Data\LazyManager;
15
use WebTheory\Saveyour\Field\DataOnly;
16
use WebTheory\Saveyour\Formatting\LazyDataFormatter;
17
use WebTheory\Saveyour\Http\Request;
18
use WebTheory\Saveyour\Report\Builder\ProcessedFieldReportBuilder;
19
use WebTheory\Saveyour\Validation\PermissiveValidator;
20
21
class FormFieldController implements FormFieldControllerInterface
22
{
23
    protected string $requestVar;
24
25
    protected FormFieldInterface $formField;
26
27
    protected FieldDataManagerInterface $dataManager;
28
29
    protected ValidatorInterface $validator;
30
31
    protected DataFormatterInterface $dataFormatter;
32
33
    protected ProcessedFieldReportBuilderInterface $cacheBuilder;
34
35
    protected bool $isPermittedToProcess = true;
36
37
    /**
38
     * @var array<string>
39
     */
40
    protected array $mustAwait = [];
41
42 78
    public function __construct(
43
        string $requestVar,
44
        ?FormFieldInterface $formField = null,
45
        ?FieldDataManagerInterface $dataManager = null,
46
        ?ValidatorInterface $validator = null,
47
        ?DataFormatterInterface $formatter = null,
48
        ?bool $processingEnabled = null,
49
        ?array $await = null
50
    ) {
51 78
        $this->requestVar = $requestVar;
52
53 78
        $this->formField = $formField ?? new DataOnly();
54 78
        $this->dataManager = $dataManager ?? new LazyManager();
55 78
        $this->validator = $validator ?? new PermissiveValidator();
56 78
        $this->dataFormatter = $formatter ?? new LazyDataFormatter();
57
58 78
        $this->isPermittedToProcess = $processingEnabled ?? $this->isPermittedToProcess;
59
60 78
        if ($await) {
61 15
            $this->setMustAwait(...$await);
62
        }
63
    }
64
65 18
    public function getRequestVar(): string
66
    {
67 18
        return $this->requestVar;
68
    }
69
70 15
    public function getFormField(): ?FormFieldInterface
71
    {
72 15
        return $this->formField;
73
    }
74
75
    /**
76
     * {@inheritDoc}
77
     */
78 15
    public function mustAwait(): array
79
    {
80 15
        return $this->mustAwait;
81
    }
82
83
    /**
84
     * @return $this
85
     */
86 15
    protected function setMustAwait(string ...$fields): FormFieldController
87
    {
88 15
        $this->mustAwait = $fields;
89
90 15
        return $this;
91
    }
92
93
    /**
94
     * @return bool
95
     */
96 18
    public function isPermittedToProcess(): bool
97
    {
98 18
        return $this->isPermittedToProcess;
99
    }
100
101
    /**
102
     * {@inheritDoc}
103
     */
104
    public function inspect($value): ValidationReportInterface
105
    {
106
        return $this->validator->inspect($value);
107
    }
108
109
    /**
110
     * {@inheritDoc}
111
     */
112 9
    public function validate($value): bool
113
    {
114 9
        return $this->validator->validate($value);
115
    }
116
117
    /**
118
     * {@inheritDoc}
119
     */
120 6
    public function process(ServerRequestInterface $request): ProcessedFieldReportInterface
121
    {
122 6
        $this->initCacheBuilder();
123 6
        $this->processData($request);
124
125 6
        return $this->cacheBuilder->build();
126
    }
127
128
    /**
129
     * @return $this
130
     */
131 6
    protected function initCacheBuilder(): FormFieldController
132
    {
133 6
        $this->cacheBuilder = new ProcessedFieldReportBuilder();
134
135 6
        return $this;
136
    }
137
138
    /**
139
     * @return $this
140
     */
141 6
    protected function processData(ServerRequestInterface $request): FormFieldController
142
    {
143 6
        $filteredInput = $this->getUpdatedValue($request);
144
145 6
        $this->cacheBuilder->withSanitizedInputValue($filteredInput);
146
147 6
        if (false !== $filteredInput && $this->isPermittedToProcess()) {
148 5
            $updated = $this->processInput($request, $filteredInput);
149
150 5
            $this->cacheBuilder->withUpdateAttempted(true)->withUpdateSuccessful($updated);
151
        }
152
153 6
        return $this;
154
    }
155
156 5
    protected function processInput(ServerRequestInterface $request, $input): bool
157
    {
158 5
        return $this->dataManager->handleSubmittedData($request, $this->formatInput($input));
159
    }
160
161 9
    public function getPresetValue(ServerRequestInterface $request)
162
    {
163 9
        return $this->formatData($this->dataManager->getCurrentData($request));
164
    }
165
166 6
    public function requestVarPresent(ServerRequestInterface $request): bool
167
    {
168 6
        return Request::has($request, $this->requestVar);
169
    }
170
171 6
    protected function getRawInput(ServerRequestInterface $request)
172
    {
173 6
        return Request::var($request, $this->requestVar);
174
    }
175
176 6
    public function getUpdatedValue(ServerRequestInterface $request)
177
    {
178 6
        return $this->formatInput($this->getRawInput($request));
179
    }
180
181 9
    protected function formatData($data)
182
    {
183 9
        return $this->dataFormatter->formatData($data);
184
    }
185
186 6
    protected function formatInput($input)
187
    {
188 6
        return $this->dataFormatter->formatInput($input);
189
    }
190
191 3
    public function render(ServerRequestInterface $request): string
192
    {
193 3
        return $this->compose($request)->toHtml();
194
    }
195
196 9
    public function compose(ServerRequestInterface $request): FormFieldInterface
197
    {
198 9
        return $this->normalizeFormField($request)->getFormField();
199
    }
200
201 9
    protected function normalizeFormField(ServerRequestInterface $request): FormFieldController
202
    {
203 9
        return $this->setFormFieldName()->setFormFieldValue($request);
204
    }
205
206
    /**
207
     * @return $this
208
     */
209 9
    protected function setFormFieldName(): FormFieldController
210
    {
211 9
        $this->formField->setName($this->getRequestVar());
212
213 9
        return $this;
214
    }
215
216
    /**
217
     * @return $this
218
     */
219 9
    public function setFormFieldValue(ServerRequestInterface $request): FormFieldController
220
    {
221 9
        $this->formField->setValue($this->getPresetValue($request));
222
223 9
        return $this;
224
    }
225
}
226