Test Setup Failed
Push — master ( 3f4bf8...f5ce5c )
by La Teva Web
03:23
created

QueryUpdater::allowedField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
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\Field\AbstractField;
9
use LaTevaWeb\QueryUpdater\Field\SimpleField;
10
11
class QueryUpdater
12
{
13
    protected $allowedFields;
14
    protected $data;
15
    protected $model;
16
17
    private function __construct(Model $model, $request = null)
18
    {
19
        $this->model = $model;
20
21
        $this->data = $this->requestToData($request);
22
    }
23
24
    private function requestToData($request): array
25
    {
26
        if ($request instanceof Request) {
27
            return $request->all();
28
        }
29
30
        if (is_array($request)) {
31
            return $request;
32
        }
33
34
        return [];
35
    }
36
37
    public static function for(Model $model, $request = null): self
38
    {
39
        return new static($model, $request ?? request());
40
    }
41
42
    public function updateFields(array $fields = [])
43
    {
44
        $fields = $this->composeFields($fields);
45
46
        $fields->each(function ($field) {
47
            if ($this->fieldExists($field->getName()) && $this->allowedField($field->getName())) {
48
                $this->model->{$field->getName()} = $field->getValue();
49
            }
50
        });
51
52
        return $this->model;
53
    }
54
55
    private function allowedField(string $name = null): bool
56
    {
57
        return in_array($name, $this->allowedFields);
58
    }
59
60
    private function fieldExists(string $name = null): bool
61
    {
62
        return $this->model
63
            ->getConnection()
64
            ->getSchemaBuilder()
65
            ->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
    private function composeFields(array $fields = []): Collection
76
    {
77
        return collect($fields)
78
            ->flatten()
79
            ->map(function ($field) {
80
                if (empty($field)) {
81
                    return false;
82
                } else {
83
                    if (is_string($field)) {
84
                        $this->setAllowedField($field);
85
                        return SimpleField::field($field, $this->getDataValue($field));
86
                    }
87
88
                    $this->setAllowedField($field->getName());
89
                    $field->setValue($this->getDataValue($field->getName()));
90
                    $field->setStoredValue($this->model->{$field->getName()});
91
                    return $field;
92
                }
93
            })
94
            ->filter(function ($field) {
95
                return $field instanceof AbstractField;
96
            });
97
    }
98
99
    private function getDataValue($name)
100
    {
101
        return $this->data[$name] ?? null;
102
    }
103
104
    private function setAllowedField($name)
105
    {
106
        $this->allowedFields[] = $name;
107
    }
108
}
109