QueryUpdater   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Test Coverage

Coverage 93.18%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 40
c 1
b 1
f 0
dl 0
loc 98
ccs 41
cts 44
cp 0.9318
rs 10
wmc 15

9 Methods

Rating   Name   Duplication   Size   Complexity  
A composeFields() 0 23 3
A fieldExists() 0 6 1
A updateFields() 0 11 3
A setAllowedField() 0 3 1
A requestToData() 0 11 3
A allowedField() 0 3 1
A __construct() 0 5 1
A getDataValue() 0 3 1
A for() 0 3 1
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