Options::create()   A
last analyzed

Complexity

Conditions 2
Paths 7

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 7
nop 0
1
<?php namespace Bedard\Shop\Controllers;
2
3
use Bedard\Shop\Classes\BackendController;
4
use Bedard\Shop\Models\Option;
5
use Bedard\Shop\Models\Product;
6
use Exception;
7
use Response;
8
9
/**
10
 * Categories Back-end Controller.
11
 */
12
class Options extends BackendController
13
{
14
    /**
15
     * Create a new option.
16
     *
17
     * @return Response
18
     */
19
    public function create()
20
    {
21
        try {
22
            // create our new option
23
            $option = input('option');
24
            unset($option['id']);
25
            $model = Option::create($option);
26
            $model->load('values');
27
28
            // attach it to the product with a deferred binding
29
            $product = new Product;
30
            $product->options()->add($model, uniqid('session_key', true));
31
32
            // return the new option
33
            return Response::make($model, 200);
34
        } catch (Exception $e) {
35
            // if anything went wrong, send back the error message
36
            return Response::make($e->getMessage(), 500);
37
        }
38
    }
39
40
    /**
41
     * Validate an option.
42
     *
43
     * @return Response
44
     */
45 View Code Duplication
    public function validate()
46
    {
47
        try {
48
            $data = input('option');
49
            $option = Option::with('values')->findOrNew($data['id']);
50
            $option->fill($data);
51
            $option->validate();
52
53
            return Response::make($option, 200);
54
        } catch (Exception $e) {
55
            return Response::make($e->getMessage(), 500);
56
        }
57
    }
58
}
59