Completed
Push — master ( 114eca...839184 )
by Ryan
07:05
created

FormCriteria::builder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php namespace Anomaly\Streams\Platform\Ui\Form;
2
3
use Anomaly\Streams\Platform\Support\Decorator;
4
use Anomaly\Streams\Platform\Support\Hydrator;
5
use Anomaly\Streams\Platform\Traits\FiresCallbacks;
6
use Illuminate\Contracts\Cache\Repository;
7
use Illuminate\Http\Request;
8
9
/**
10
 * Class FormCriteria
11
 *
12
 * @link          http://anomaly.is/streams-platform
13
 * @author        AnomalyLabs, Inc. <[email protected]>
14
 * @author        Ryan Thompson <[email protected]>
15
 * @package       Anomaly\Streams\Platform\Ui\Form
16
 */
17
class FormCriteria
18
{
19
20
    use FiresCallbacks;
21
22
    /**
23
     * The cache repository.
24
     *
25
     * @var Repository
26
     */
27
    protected $cache;
28
29
    /**
30
     * The form builder.
31
     *
32
     * @var FormBuilder
33
     */
34
    protected $builder;
35
36
    /**
37
     * The request object.
38
     *
39
     * @var Request
40
     */
41
    protected $request;
42
43
    /**
44
     * The hydrator utility.
45
     *
46
     * @var Hydrator
47
     */
48
    protected $hydrator;
49
50
    /**
51
     * The parameters.
52
     *
53
     * @var array
54
     */
55
    protected $parameters = [];
56
57
    /**
58
     * Create a new FormCriteria instance.
59
     *
60
     * @param Repository  $cache
61
     * @param Request     $request
62
     * @param Hydrator    $hydrator
63
     * @param FormBuilder $builder
64
     * @param array       $parameters
65
     */
66
    public function __construct(
67
        Repository $cache,
68
        Request $request,
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...
69
        Hydrator $hydrator,
70
        FormBuilder $builder,
71
        array $parameters = []
72
    ) {
73
        $this->cache      = $cache;
74
        $this->builder    = $builder;
75
        $this->request    = $request;
76
        $this->hydrator   = $hydrator;
77
        $this->parameters = $parameters;
78
79
        $this->setBuilder($builder);
80
    }
81
82
    /**
83
     * Get the form.
84
     *
85
     * @return FormPresenter
86
     */
87
    public function get()
88
    {
89
        $this->build();
90
91
        return (new Decorator())->decorate($this->builder->make()->getForm());
0 ignored issues
show
Bug Best Practice introduced by
The return type of return (new \Anomaly\Str...er->make()->getForm()); (object|integer|double|string|null|boolean|array) is incompatible with the return type documented by Anomaly\Streams\Platform\Ui\Form\FormCriteria::get of type Anomaly\Streams\Platform...Form\FormPresenter|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
92
    }
93
94
    /**
95
     * Build the builder.
96
     *
97
     * @return FormBuilder
98
     */
99
    public function build()
100
    {
101
102
        /**
103
         * Hide breadcrumbs by default.
104
         */
105
        array_set(
106
            $this->parameters,
107
            'options.breadcrumb',
108
            array_get(
109
                $this->parameters,
110
                'options.breadcrumb',
111
                false
112
            )
113
        );
114
115
        /**
116
         * Cache and hash!
117
         */
118
        array_set($this->parameters, 'key', md5(json_encode($this->parameters)));
119
120
        /**
121
         * Set the forms URL after obtaining
122
         * our parameter hash for the form.
123
         */
124
        array_set(
125
            $this->parameters,
126
            'options.url',
127
            array_get(
128
                $this->parameters,
129
                'options.url',
130
                $this->builder->getOption('url', 'form/handle/' . array_get($this->parameters, 'key'))
131
            )
132
        );
133
134
        $this->cache->remember(
135
            'form::' . array_get($this->parameters, 'key'),
136
            30,
137
            function () {
138
                return $this->parameters;
139
            }
140
        );
141
142
        return $this->hydrator->hydrate($this->builder, $this->parameters);
143
    }
144
145
    /**
146
     * Set a parameter.
147
     *
148
     * @param $key
149
     * @param $value
150
     * @return $this
151
     */
152
    public function setParameter($key, $value)
153
    {
154
        $this->parameters[$key] = $value;
155
156
        return $this;
157
    }
158
159
    /**
160
     * Get the builder.
161
     *
162
     * @return FormBuilder
163
     */
164
    public function getBuilder()
165
    {
166
        return $this->builder;
167
    }
168
169
    /**
170
     * Set the form builder.
171
     *
172
     * @param FormBuilder $builder
173
     * @return $this
174
     */
175
    public function setBuilder($builder)
176
    {
177
        if (!is_object($builder)) {
178
            $builder = app($builder);
179
        }
180
181
        $this->builder = $builder;
182
183
        array_set($this->parameters, 'builder', get_class($this->builder));
184
185
        return $this;
186
    }
187
188
    /**
189
     * Route through __get
190
     *
191
     * @param $name
192
     * @return $this
193
     */
194
    public function __get($name)
195
    {
196
        return $this->__call($name, []);
197
    }
198
199
    /**
200
     * @param $name
201
     * @param $arguments
202
     * @return $this
203
     */
204
    public function __call($name, $arguments)
205
    {
206
207 View Code Duplication
        if (method_exists($this->builder, camel_case('set_' . $name))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
208
209
            array_set($this->parameters, $name, array_shift($arguments));
210
211
            return $this;
212
        }
213
214 View Code Duplication
        if (method_exists($this->builder, camel_case('add_' . $name))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
215
216
            array_set($this->parameters, $name, array_shift($arguments));
217
218
            return $this;
219
        }
220
221 View Code Duplication
        if (!method_exists($this->builder, camel_case($name)) && count($arguments) === 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
222
223
            $key = snake_case($name);
224
225
            array_set($this->parameters, "options.{$key}", array_shift($arguments));
226
227
            return $this;
228
        }
229
230 View Code Duplication
        if (!method_exists($this->builder, camel_case($name)) && count($arguments) === 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
231
232
            $key = snake_case($name);
233
234
            // Helpful for form.disableLabels().disableFoo() ...
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
235
            array_set($this->parameters, "options.{$key}", true);
236
237
            return $this;
238
        }
239
240
        return $this;
241
    }
242
243
    /**
244
     * Return the form.
245
     *
246
     * @return string
247
     */
248
    public function __toString()
249
    {
250
        return $this->get()->__toString();
251
    }
252
}
253