QueryUpdater::fieldExists()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 1
rs 10
1
<?php
2
3
namespace LaTevaWeb\QueryUpdater;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Collection;
8
use LaTevaWeb\QueryUpdater\Filter\AbstractFilter;
9
use LaTevaWeb\QueryUpdater\Filter\SimpleFilter;
10
11
class QueryUpdater
12
{
13
    protected $allowedFields;
14
    protected $data;
15
    protected $model;
16
17 4
    private function __construct(Model $model, $request = null)
18
    {
19 4
        $this->model = $model;
20
21 4
        $this->data = $this->requestToData($request);
22 4
    }
23
24 4
    private function requestToData($request): array
25
    {
26 4
        if ($request instanceof Request) {
27
            return $request->all();
28
        }
29
30 4
        if (is_array($request)) {
31 4
            return $request;
32
        }
33
34
        return [];
35
    }
36
37 4
    public static function for(Model $model, $request = null): self
38
    {
39 4
        return new static($model, $request ?? request());
40
    }
41
42 4
    public function updateFields(array $fields = [])
43
    {
44 4
        $fields = $this->composeFields($fields);
45
46
        $fields->each(function ($field) {
47 4
            if ($this->fieldExists($field->getName()) && $this->allowedField($field->getName())) {
48 4
                $this->model->{$field->getName()} = $field->getValue();
49
            }
50 4
        });
51
52 4
        return $this->model;
53
    }
54
55 4
    private function allowedField(string $name = null): bool
56
    {
57 4
        return in_array($name, $this->allowedFields);
58
    }
59
60 4
    private function fieldExists(string $name = null): bool
61
    {
62 4
        return $this->model
63 4
            ->getConnection()
64 4
            ->getSchemaBuilder()
65 4
            ->hasColumn($this->model->getTable(), $name);
66
    }
67
68
    /**
69
     * Compose array of fields (string or object) to array of AbstractField objects.
70
     *
71
     * @param array
72
     *
73
     * @return Collection
74
     */
75 4
    private function composeFields(array $fields = []): Collection
76
    {
77 4
        return collect($fields)
78 4
            ->flatten()
79
            ->map(function ($field) {
80 4
                if (empty($field)) {
81
                    return false;
82
                } else {
83 4
                    if (is_string($field)) {
84 3
                        $this->setAllowedField($field);
85
86 3
                        return SimpleFilter::field($field, $this->getDataValue($field));
87
                    }
88
89 1
                    $this->setAllowedField($field->getName());
90 1
                    $field->setValue($this->getDataValue($field->getName()));
91 1
                    $field->setStoredValue($this->model->{$field->getName()});
92
93 1
                    return $field;
94
                }
95 4
            })
96
            ->filter(function ($field) {
97 4
                return $field instanceof AbstractFilter;
98 4
            });
99
    }
100
101 4
    private function getDataValue($name)
102
    {
103 4
        return $this->data[$name] ?? null;
104
    }
105
106 4
    private function setAllowedField($name)
107
    {
108 4
        $this->allowedFields[] = $name;
109 4
    }
110
}
111