Completed
Push — master ( e1110a...9fbab8 )
by Scott
05:23
created

Options::createValue()   A

Complexity

Conditions 4
Paths 7

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
rs 9.2
cc 4
eloc 12
nc 7
nop 0
1
<?php namespace Bedard\Shop\Controllers;
2
3
use Backend\Classes\Controller;
4
use Bedard\Shop\Models\Option;
5
use Bedard\Shop\Models\OptionValue;
6
use Exception;
7
use Response;
8
9
/**
10
 * Options Back-end Controller.
11
 */
12
class Options extends Controller
13
{
14
    public function createValue()
15
    {
16
        $data = input('value');
17
        if (! $data || ! is_array($data)) {
18
            return Response::make('Error', 422);
19
        }
20
21
        try {
22
            $sessionKey = uniqid('session_key', true);
23
            $value = OptionValue::create($data);
24
            $option = new Option;
25
            $option->values()->add($value, $sessionKey);
26
27
            return Response::make($value, 202);
28
        } catch (Exception $e) {
29
            return Response::make($e->getMessage(), 500);
30
        }
31
    }
32
33
    /**
34
     * Validate an option.
35
     *
36
     * @return Response
37
     */
38
    public function validate()
39
    {
40
        $data = input('option');
41
        if (! $data || ! is_array($data)) {
42
            return Response::make('Error', 422);
43
        }
44
45
        try {
46
            $option = new Option($data);
47
            $option->validate();
48
        } catch (Exception $e) {
49
            return Response::make($e->getMessage(), 400);
50
        }
51
52
        return Response::make($option, 200);
53
    }
54
}
55