Passed
Push — main ( 157d12...3c2c89 )
by Garbuz
02:36
created

Field   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 86.67%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 7
eloc 12
c 3
b 0
f 0
dl 0
loc 46
ccs 13
cts 15
cp 0.8667
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A __call() 0 7 2
A findFieldClass() 0 10 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GarbuzIvan\LaravelGeneratorPackage\Form;
6
7
use GarbuzIvan\LaravelGeneratorPackage\Configuration;
8
use GarbuzIvan\LaravelGeneratorPackage\Contracts\FieldInterface;
9
use GarbuzIvan\LaravelGeneratorPackage\Exceptions\FieldDoesNotExistsException;
10
11
class Field
12
{
13
    protected Configuration $config;
14
15
    /**
16
     * Form constructor.
17
     * @param Configuration $config
18
     */
19 15
    public function __construct(Configuration $config)
20
    {
21 15
        $this->config = $config;
22 15
    }
23
24
    /**
25
     * Find field class.
26
     *
27
     * @param string $method
28
     * @return bool|string
29
     */
30 15
    public function findFieldClass(string $method)
31
    {
32 15
        $fields = $this->config->getFields();
33
        if (isset($fields[$method])) {
34
            $class = $fields[$method];
35 15
        }
36 15
        if (isset($class) && class_exists($class)) {
37 15
            return $class;
38
        }
39 15
        return false;
40 15
    }
41
42
    /**
43
     * Generate a Field object and add to form builder if Field exists.
44
     *
45
     * @param string $method
46
     * @param $args
47
     * @return FieldInterface
48
     * @throws FieldDoesNotExistsException
49
     */
50
    public function __call(string $method, array $args): FieldInterface
51
    {
52
        if ($className = $this->findFieldClass($method)) {
53 15
            /** @scrutinizer ignore-call */
54
            return app($className)->init($args);
55 15
        }
56 15
        throw new FieldDoesNotExistsException();
57
    }
58
}
59