Passed
Push — main ( c30533...5fffd3 )
by Garbuz
03:00
created

Field   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

3 Methods

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