HasForm::scaffoldForm()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 3
eloc 13
c 2
b 1
f 0
nc 2
nop 0
dl 0
loc 22
ccs 0
cts 13
cp 0
crap 12
rs 9.8333
1
<?php
2
3
namespace Terranet\Administrator\Traits\Module;
4
5
use Terranet\Administrator\Decorators\Grid;
6
use Terranet\Administrator\Form\Collection\Mutable;
7
use Terranet\Translatable\Translatable;
8
9
trait HasForm
10
{
11
    /**
12
     * Provides array of editable columns.
13
     *
14
     * @return Mutable
15
     */
16
    public function form()
17
    {
18
        return $this->scaffoldForm();
19
    }
20
21
    /**
22
     * Build editable columns based on table columns metadata.
23
     *
24
     * @return Mutable
25
     */
26
    protected function scaffoldForm()
27
    {
28
        $editable = new Mutable();
29
30
        if ($eloquent = $this->model()) {
0 ignored issues
show
Bug introduced by
It seems like model() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

30
        if ($eloquent = $this->/** @scrutinizer ignore-call */ model()) {
Loading history...
31
            $editable = $editable
32
                ->merge([$eloquent->getKeyName()])
33
                ->merge($translatable = $this->scaffoldTranslatable($eloquent))
34
                ->merge($eloquent->getFillable());
35
36
            return $editable->build(
37
                new Grid($eloquent)
38
            )->map(function ($element) use ($translatable) {
39
                if (\in_array($element->id(), $translatable, true)) {
40
                    return $element->translatable();
41
                }
42
43
                return $element;
44
            });
45
        }
46
47
        return $editable;
48
    }
49
50
    /**
51
     * @param $eloquent
52
     * @return array
53
     */
54
    protected function scaffoldTranslatable($eloquent)
55
    {
56
        return ($eloquent instanceof Translatable && method_exists($eloquent, 'getTranslatedAttributes'))
57
            ? $eloquent->getTranslatedAttributes()
58
            : [];
59
    }
60
}
61