Options   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 27.66 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 13
loc 47
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 20 2
A validate() 13 13 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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