Passed
Push — main ( 2962a2...bc4cf3 )
by Garbuz
02:48
created

Field   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Test Coverage

Coverage 86.54%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 26
eloc 49
c 3
b 0
f 0
dl 0
loc 110
ccs 45
cts 52
cp 0.8654
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __call() 0 7 2
A findFieldClass() 0 13 5
F loadFieldFromArray() 0 53 18
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GarbuzIvan\LaravelGeneratorPackage\Form;
6
7
use Exception;
8
use GarbuzIvan\LaravelGeneratorPackage\Configuration;
9
use GarbuzIvan\LaravelGeneratorPackage\Contracts\FieldInterface;
10
use GarbuzIvan\LaravelGeneratorPackage\Exceptions\FieldDoesNotExistsException;
11
12
class Field
13
{
14
    protected Configuration $config;
15
16
    /**
17
     * Form constructor.
18
     * @param Configuration $config
19
     */
20 27
    public function __construct(Configuration $config)
21
    {
22 27
        $this->config = $config;
23 27
    }
24
25
    /**
26
     * Find field class.
27
     *
28
     * @param string $method
29
     * @return bool|string
30
     */
31 27
    public function findFieldClass(string $method)
32
    {
33 27
        if (mb_strlen(trim($method)) == 0) {
34
            return false;
35
        }
36 27
        $fields = $this->config->getFields();
37 27
        if (isset($fields[$method])) {
38 26
            $class = $fields[$method];
39
        }
40 27
        if (isset($class) && class_exists($class)) {
41 26
            return $class;
42
        }
43 2
        return false;
44
    }
45
46
    /**
47
     * Generate a Field object and add to form builder if Field exists.
48
     *
49
     * @param string $method
50
     * @param $args
51
     * @return FieldInterface
52
     * @throws FieldDoesNotExistsException
53
     */
54 22
    public function __call(string $method, array $args): FieldInterface
55
    {
56 22
        if ($className = $this->findFieldClass($method)) {
57
            /** @scrutinizer ignore-call */
58 21
            return app($className)->init($args);
59
        }
60 1
        throw new FieldDoesNotExistsException();
61
    }
62
63
    /**
64
     * @param string $column
65
     * @param array $config
66
     * @return FieldInterface
67
     * @throws Exception
68
     */
69 4
    public function loadFieldFromArray(string $column, array $config): FieldInterface
70
    {
71 4
        $className = $this->findFieldClass($config['field']);
72 4
        if (!$className) {
1 ignored issue
show
introduced by
The condition $className is always false.
Loading history...
73
            throw new Exception('Field ' . $column . ':' . $config['field'] . ' not found and not available for use');
74
        }
75 4
        $field = app($className)->init([$column, $config['label']]);
76 4
        if (isset($config['placeholder'])) {
77 4
            $field->setPlaceholder($config['placeholder']);
78
        }
79 4
        if (isset($config['default'])) {
80
            $field->default($config['default']);
81
        }
82 4
        if (isset($config['index'])) {
83 4
            $field->index($config['index']);
84
        }
85 4
        if (isset($config['fillable'])) {
86 4
            $field->fillable($config['fillable']);
87
        }
88 4
        if (isset($config['hidden'])) {
89 4
            $field->hidden($config['hidden']);
90
        }
91 4
        if (isset($config['references']['table']) && isset($config['references']['field'])) {
92
            $field->references($config['references']['table'], $config['references']['field']);
93
        }
94 4
        if (isset($config['filter']['type'])) {
95 4
            $field->setType($config['filter']['type']);
96
        }
97 4
        if (isset($config['filter']['light'])) {
98 4
            $field->setLight($config['filter']['light']);
99
        }
100 4
        if (isset($config['filter']['light'])) {
101 4
            $field->setLight($config['filter']['light']);
102
        }
103 4
        if (isset($config['filter']['nullable'])) {
104 4
            $field->nullable($config['filter']['nullable']);
105
        }
106 4
        if (isset($config['filter']['unique'])) {
107 4
            $field->nullable($config['filter']['unique']);
108
        }
109 4
        if (isset($config['filter']['required'])) {
110 4
            $field->required($config['filter']['required']);
111
        }
112 4
        if (isset($config['filter']['max'])) {
113
            $field->max($config['filter']['max']);
114
        }
115 4
        if (isset($config['filter']['min'])) {
116
            $field->min($config['filter']['min']);
117
        }
118 4
        if (isset($config['filter']['mask'])) {
119
            $field->setMask($config['filter']['mask']);
120
        }
121 4
        return $field;
122
    }
123
}
124