Completed
Push — master ( 4da56f...f86135 )
by Ryan
05:41
created

FormController::handle()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 35
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 18
nc 10
nop 3
dl 0
loc 35
rs 6.7272
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 (!$builder->hasFormErrors()) {
45
            $cache->forget('form::' . $key);
46
        }
47
48
        if ($response && $response->getStatusCode() !== 200) {
49
            return $response;
50
        }
51
52
        if ($builder->isAjax()) {
53
            return $builder->getFormResponse();
54
        }
55
56
        if ($builder->hasFormErrors()) {
57
            return $redirect->back();
58
        }
59
60
        return $response ?: $redirect->back();
61
    }
62
}
63