Completed
Push — master ( 18a4ce...7adae9 )
by Scott
02:01
created

Options   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 72
Duplicated Lines 50 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 12
c 2
b 1
f 0
lcom 0
cbo 3
dl 36
loc 72
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 18 18 4
A createValue() 18 18 4
A validate() 0 16 4

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\OptionValue;
6
use Bedard\Shop\Models\Product;
7
use Exception;
8
use Response;
9
10
/**
11
 * Options Back-end Controller.
12
 */
13
class Options extends BackendController
14
{
15
    /**
16
     * Create an option with deferred bindings to a product.
17
     *
18
     * @return Response
19
     */
20 View Code Duplication
    public function create()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
21
    {
22
        $data = input('option');
23
        if (! $data || ! is_array($data)) {
24
            return Response::make('Error', 422);
25
        }
26
27
        try {
28
            $sessionKey = uniqid('session_key', true);
29
            $option = Option::create($data);
30
            $product = new Product;
31
            $product->options()->add($option, $sessionKey);
32
33
            return Response::make($option, 202);
34
        } catch (Exception $e) {
35
            return Response::make($e->getMessage(), 500);
36
        }
37
    }
38
39
    /**
40
     * Create a value with deferred bindings to an option.
41
     *
42
     * @return Response
43
     */
44 View Code Duplication
    public function createValue()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
    {
46
        $data = input('value');
47
        if (! $data || ! is_array($data)) {
48
            return Response::make('Error', 422);
49
        }
50
51
        try {
52
            $sessionKey = uniqid('session_key', true);
53
            $value = OptionValue::create($data);
54
            $option = new Option;
55
            $option->values()->add($value, $sessionKey);
56
57
            return Response::make($value, 202);
58
        } catch (Exception $e) {
59
            return Response::make($e->getMessage(), 500);
60
        }
61
    }
62
63
    /**
64
     * Validate an option.
65
     *
66
     * @return Response
67
     */
68
    public function validate()
69
    {
70
        $data = input('option');
71
        if (! $data || ! is_array($data)) {
72
            return Response::make('Error', 422);
73
        }
74
75
        try {
76
            $option = new Option($data);
77
            $option->validate();
78
        } catch (Exception $e) {
79
            return Response::make($e->getMessage(), 400);
80
        }
81
82
        return Response::make($option, 200);
83
    }
84
}
85