AbstractFormDataProcessor::valueUpdated()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 9
ccs 4
cts 5
cp 0.8
crap 3.072
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace WebTheory\Saveyour\Processor\Abstracts;
4
5
use WebTheory\Saveyour\Contracts\Processor\FormDataProcessorInterface;
6
use WebTheory\Saveyour\Contracts\Report\ProcessedFieldReportInterface;
7
use WebTheory\Saveyour\Contracts\Report\ProcessedInputReportInterface;
8
9
abstract class AbstractFormDataProcessor implements FormDataProcessorInterface
10
{
11
    protected string $name;
12
13
    protected ?array $fields = null;
14
15 9
    public function __construct(string $name, ?array $fields)
16
    {
17 9
        $this->name = $name;
18 9
        $this->setFields($fields);
19
    }
20
21
    public function getName(): string
22
    {
23
        return $this->name;
24
    }
25
26
    public function getFields(): ?array
27
    {
28
        return $this->fields;
29
    }
30
31 9
    protected function setFields(?array $fields): void
32
    {
33 9
        if (empty($fields)) {
34
            $this->fields = $fields;
35
        } else {
36 9
            foreach ($fields as $field => $param) {
37 9
                $this->addField($field, $param);
38
            }
39
        }
40
    }
41
42 9
    protected function addField(string $id, string $field): void
43
    {
44 9
        $this->fields[$id] = $field;
45
    }
46
47
    /**
48
     * @param array<string,ProcessedFieldReportInterface> $results
49
     */
50
    protected function hasReasonToProcess(array $results): bool
51
    {
52
        return $this->hasNoFields() || $this->valueUpdated($results);
53
    }
54
55
    protected function hasNoFields(): bool
56
    {
57
        return is_array($fields = $this->getFields()) && empty($fields);
58
    }
59
60
    /**
61
     * @param array<string,ProcessedFieldReportInterface> $results
62
     */
63 9
    protected function valueUpdated(array $results): bool
64
    {
65 9
        foreach ($results as $result) {
66 9
            if ($result->updateSuccessful()) {
67 9
                return true;
68
            }
69
        }
70
71
        return false;
72
    }
73
74
    /**
75
     * @param array<string,ProcessedInputReportInterface> $results
76
     */
77
    protected function allFieldsPresent(array $results): bool
78
    {
79
        foreach ($this->fields ?? array_keys($results) as $field) {
80
            $field = $results[$field] ?? null;
81
82
            if (!$field || !$this->fieldIsPresentAndValid($field)) {
83
                return false;
84
            }
85
        }
86
87
        return true;
88
    }
89
90
    protected function fieldIsPresentAndValid(ProcessedInputReportInterface $report): bool
91
    {
92
        return $report->requestVarPresent() && $report->validationStatus();
93
    }
94
95
    /**
96
     * @param array<string,ProcessedInputReportInterface> $results
97
     */
98 9
    protected function extractValues(array $results): array
99
    {
100 9
        $values = [];
101 9
        $keys = array_flip($this->fields);
0 ignored issues
show
Bug introduced by
It seems like $this->fields can also be of type null; however, parameter $array of array_flip() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

101
        $keys = array_flip(/** @scrutinizer ignore-type */ $this->fields);
Loading history...
102
103 9
        foreach ($results as $id => $report) {
104 9
            $values[$keys[$id]] = $report->rawInputValue();
105
        }
106
107 9
        return $values;
108
    }
109
110
    /**
111
     * @param array<string,ProcessedInputReportInterface> $results
112
     */
113
    protected function mappedValues(array $results)
114
    {
115
        $values = [];
116
117
        foreach ($results as $field => $report) {
118
            $values[$field] = $report->rawInputValue();
119
        }
120
121
        return $values;
122
    }
123
}
124