Completed
Push — master ( 7e75f8...53d3fb )
by Ryan
10:44 queued 05:05
created

FormController::handle()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 16
nc 5
nop 3
dl 0
loc 31
rs 8.439
c 0
b 0
f 0
1
<?php namespace Anomaly\Streams\Platform\Http\Controller;
2
3
use Anomaly\Streams\Platform\Ui\Form\Command\GetFormCriteria;
4
use Anomaly\Streams\Platform\Ui\Form\FormBuilder;
5
use Anomaly\Streams\Platform\Ui\Form\FormCriteria;
6
use Illuminate\Cache\Repository;
7
use Illuminate\Routing\Redirector;
8
9
/**
10
 * Class FormController
11
 *
12
 * @link   http://pyrocms.com/
13
 * @author PyroCMS, Inc. <[email protected]>
14
 * @author Ryan Thompson <[email protected]>
15
 */
16
class FormController extends PublicController
17
{
18
19
    /**
20
     * Handle the form.
21
     *
22
     * @param  Repository                                 $cache
23
     * @param  Redirector                                 $redirect
24
     * @param                                             $key
25
     * @return \Symfony\Component\HttpFoundation\Response
26
     */
27
    public function handle(Repository $cache, Redirector $redirect, $key)
28
    {
29
        $parameters = $cache->get('form::' . $key);
30
31
        /* @var FormCriteria $criteria */
32
        $criteria = $this->dispatch(new GetFormCriteria($parameters));
33
34
        /* @var FormBuilder $builder */
35
        $builder = $criteria->build();
36
37
        $response = $builder
38
            ->build()
39
            ->post()
40
            ->getFormResponse();
41
42
        $builder->flash();
43
44
        if ($response && $response->getStatusCode() !== 200) {
45
            return $response;
46
        }
47
48
        if ($builder->isAjax()) {
49
            return $builder->getFormResponse();
50
        }
51
52
        if ($builder->hasFormErrors()) {
53
            return $redirect->back();
54
        }
55
56
        return $response ?: $redirect->back();
57
    }
58
}
59