Completed
Push — master ( bfcf14...e377ec )
by Ryan
07:17
created

FieldFactory::make()   B

Complexity

Conditions 8
Paths 4

Size

Total Lines 54
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 54
rs 7.4119
cc 8
eloc 27
nc 4
nop 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php namespace Anomaly\Streams\Platform\Ui\Form\Component\Field;
2
3
use Anomaly\Streams\Platform\Addon\FieldType\FieldType;
4
use Anomaly\Streams\Platform\Addon\FieldType\FieldTypeBuilder;
5
use Anomaly\Streams\Platform\Entry\Contract\EntryInterface;
6
use Anomaly\Streams\Platform\Stream\Contract\StreamInterface;
7
use Anomaly\Streams\Platform\Support\Hydrator;
8
use Illuminate\Http\Request;
9
10
/**
11
 * Class FieldFactory
12
 *
13
 * @link          http://anomaly.is/streams-platform
14
 * @author        AnomalyLabs, Inc. <[email protected]>
15
 * @author        Ryan Thompson <[email protected]>
16
 * @package       Anomaly\Streams\Platform\Ui\Form\Component\Field
17
 */
18
class FieldFactory
19
{
20
21
    /**
22
     * The field type builder utility.
23
     *
24
     * @var FieldTypeBuilder
25
     */
26
    protected $builder;
27
28
    /**
29
     * The request object.
30
     *
31
     * @var Request
32
     */
33
    protected $request;
34
35
    /**
36
     * The hydrator utility.
37
     *
38
     * @var Hydrator
39
     */
40
    protected $hydrator;
41
42
    /**
43
     * Create a new FieldFactory instance.
44
     *
45
     * @param FieldTypeBuilder $builder
46
     * @param Request          $request
47
     * @param Hydrator         $hydrator
48
     */
49
    public function __construct(FieldTypeBuilder $builder, Request $request, Hydrator $hydrator)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
50
    {
51
        $this->builder  = $builder;
52
        $this->request  = $request;
53
        $this->hydrator = $hydrator;
54
    }
55
56
    /**
57
     * Make a field type.
58
     *
59
     * @param array           $parameters
60
     * @param StreamInterface $stream
61
     * @param null            $entry
62
     * @return FieldType
63
     */
64
    public function make(array $parameters, StreamInterface $stream = null, $entry = null)
65
    {
66
        /* @var EntryInterface $entry */
67
        if ($stream && $entry instanceof EntryInterface && $entry->hasField(array_get($parameters, 'field'))) {
68
69
            /**
70
             * Allow overriding the type here
71
             * should they want to do that.
72
             */
73
            if (array_get($parameters, 'type')) {
74
                $field = $this->builder->build($parameters);
75
            } else {
76
                $field = clone($entry->getFieldType(array_get($parameters, 'field')));
77
            }
78
79
            $modifier = $field->getModifier();
80
81
            $value = array_pull($parameters, 'value');
82
83
            /* @var EntryInterface $entry */
84
            $field->setValue(
85
                (!is_null($value)) ? $modifier->restore($value) : $entry->getAttribute($field->getField())
86
            );
87
        } elseif (is_object($entry)) {
88
89
            $field    = $this->builder->build($parameters);
90
            $modifier = $field->getModifier();
91
92
            $value = array_pull($parameters, 'value');
93
94
            $field->setValue((!is_null($value)) ? $modifier->restore($value) : $entry->{$field->getField()});
95
        } else {
96
97
            $field    = $this->builder->build($parameters);
98
            $modifier = $field->getModifier();
99
100
            $field->setValue($modifier->restore(array_pull($parameters, 'value')));
101
        }
102
103
        // Set the entry.
104
        $field->setEntry($entry);
105
106
        // Merge in rules and validators.
107
        $field
108
            ->mergeRules(array_pull($parameters, 'rules', []))
109
            ->mergeConfig(array_pull($parameters, 'config', []))
110
            ->mergeMessages(array_pull($parameters, 'messages', []))
111
            ->mergeValidators(array_pull($parameters, 'validators', []));
112
113
        // Hydrate the field with parameters.
114
        $this->hydrator->hydrate($field, $parameters);
115
116
        return $field;
117
    }
118
}
119