Passed
Push — master ( f4db78...b47fe7 )
by Chris
07:23
created

FormFieldController::normalizeFormField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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<int,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
        $await && $this->setMustAwait(...$await);
61
    }
62
63 18
    public function getRequestVar(): string
64
    {
65 18
        return $this->requestVar;
66
    }
67
68 15
    public function getFormField(): ?FormFieldInterface
69
    {
70 15
        return $this->formField;
71
    }
72
73
    /**
74
     * Get the value of mustAwait
75
     *
76
     * @return string[]
77
     */
78 15
    public function mustAwait(): array
79
    {
80 15
        return $this->mustAwait;
81
    }
82
83
    /**
84
     * Set the value of mustAwait
85
     *
86
     * @param string[] $mustAwait
87
     *
88
     * @return self
89
     */
90 15
    protected function setMustAwait(string ...$fields)
91
    {
92 15
        $this->mustAwait = $fields;
93
94 15
        return $this;
95
    }
96
97
    /**
98
     * Get the value of savingDisabled
99
     *
100
     * @return bool
101
     */
102 18
    public function isPermittedToProcess(): bool
103
    {
104 18
        return $this->isPermittedToProcess;
105
    }
106
107
    /**
108
     * {@inheritDoc}
109
     */
110
    public function inspect($value): ValidationReportInterface
111
    {
112
        return $this->validator->inspect($value);
113
    }
114
115
    /**
116
     * {@inheritDoc}
117
     */
118 9
    public function validate($value): bool
119
    {
120 9
        return $this->validator->validate($value);
121
    }
122
123
    /**
124
     * {@inheritDoc}
125
     */
126 6
    public function process(ServerRequestInterface $request): ProcessedFieldReportInterface
127
    {
128 6
        $this->initCacheBuilder();
129 6
        $this->processData($request);
130
131 6
        return $this->cacheBuilder->build();
132
    }
133
134 6
    protected function initCacheBuilder()
135
    {
136 6
        $this->cacheBuilder = new ProcessedFieldReportBuilder();
137
138 6
        return $this;
139
    }
140
141 6
    protected function processData(ServerRequestInterface $request)
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 9
    protected function setFormFieldName(): FormFieldController
207
    {
208 9
        $this->formField->setName($this->getRequestVar());
209
210 9
        return $this;
211
    }
212
213 9
    public function setFormFieldValue(ServerRequestInterface $request): FormFieldController
214
    {
215 9
        $this->formField->setValue($this->getPresetValue($request));
216
217 9
        return $this;
218
    }
219
}
220