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

Options   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 8
c 2
b 1
f 0
lcom 0
cbo 1
dl 0
loc 43
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createValue() 0 18 4
A validate() 0 16 4
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