Passed
Push — main ( 317075...c1e860 )
by Garbuz
02:43
created

Field   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

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 14
cp 0.9286
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 18
    public function __construct(Configuration $config)
20
    {
21 18
        $this->config = $config;
22 18
    }
23
24
    /**
25
     * Find field class.
26
     *
27
     * @param string $method
28
     * @return bool|string
29
     */
30 18
    public function findFieldClass(string $method)
31
    {
32 18
        $fields = $this->config->getFields();
33 18
        if (isset($fields[$method])) {
34 18
            $class = $fields[$method];
35
        }
36 18
        if (isset($class) && class_exists($class)) {
37 18
            return $class;
38
        }
39 1
        return false;
40
    }
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 17
    public function __call(string $method, array $args): FieldInterface
51
    {
52 17
        if ($className = $this->findFieldClass($method)) {
53
            /** @scrutinizer ignore-call */
54 17
            return app($className)->init($args);
55
        }
56
        throw new FieldDoesNotExistsException();
57
    }
58
}
59