Passed
Push — master ( 4c5faf...429320 )
by Chris
04:23
created

FieldFactory::__call()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
namespace WebTheory\Saveyour\Factories;
4
5
use WebTheory\GuctilityBelt\Traits\SmartFactoryTrait;
6
use WebTheory\Saveyour\Contracts\FieldDataManagerInterface;
7
use WebTheory\Saveyour\Contracts\FieldDataManagerResolverFactoryInterface as iDataManagerFactory;
8
use WebTheory\Saveyour\Contracts\FormFieldControllerInterface;
9
use WebTheory\Saveyour\Contracts\FormFieldInterface;
10
use WebTheory\Saveyour\Contracts\FormFieldResolverFactoryInterface as iFormFieldFactory;
11
use WebTheory\Saveyour\Controllers\FormFieldController;
12
13
class FieldFactory
14
{
15
    use SmartFactoryTrait;
0 ignored issues
show
Bug introduced by
The trait WebTheory\GuctilityBelt\Traits\SmartFactoryTrait requires the property $name which is not provided by WebTheory\Saveyour\Factories\FieldFactory.
Loading history...
16
17
    /**
18
     * @var iFormFieldFactory
19
     */
20
    protected $formFieldFactory;
21
22
    /**
23
     * @var iDataManagerFactory
24
     */
25
    protected $dataManagerFactory;
26
27
    /**
28
     *
29
     */
30
    protected $controller = FormFieldController::class;
31
32
    /**
33
     *
34
     */
35 6
    public function __construct(iFormFieldFactory $formFieldFactory, iDataManagerFactory $dataManagerFactory, ?string $controller = null)
36
    {
37 6
        $this->formFieldFactory = $formFieldFactory;
38 6
        $this->dataManagerFactory = $dataManagerFactory;
39
40 6
        if (isset($controller)) {
41
            $this->controller = $controller;
42
        }
43 6
    }
44
45
    /**
46
     *
47
     */
48 6
    public function create(array $args): FormFieldControllerInterface
49
    {
50 6
        if (isset($args['type'])) {
51 6
            $args['form_field'] = $this->createFormField($args['type']);
52
        }
53
54 6
        if (isset($args['data'])) {
55 6
            $args['data_manager'] = $this->createDataManager($args['data']);
56
        }
57
58 6
        unset($args['type'], $args['data']);
59
60 6
        return $this->createController($args);
61
    }
62
63
    /**
64
     *
65
     */
66 6
    protected function createController(array $args): FormFieldControllerInterface
67
    {
68 6
        return $this->build($this->controller, $args);
69
    }
70
71
    /**
72
     *
73
     */
74 6
    protected function createFormField(array $args): FormFieldInterface
75
    {
76 6
        $type = $args['@create'];
77 6
        unset($args['@create']);
78
79 6
        return $this->formFieldFactory->create($type, $args);
80
    }
81
82
    /**
83
     *
84
     */
85 6
    protected function createDataManager(array $args): FieldDataManagerInterface
86
    {
87 6
        $manager = $args['@create'];
88 6
        unset($args['@create']);
89
90 6
        return $this->dataManagerFactory->create($manager, $args);
91
    }
92
93
    /**
94
     *
95
     */
96 3
    public function __call($name, $args)
97
    {
98 3
        $args = $args[0];
99 3
        $args['type']['@create'] = $this->getArg($name);
100
101 3
        return $this->create($args);
102
    }
103
}
104