|
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() |
|
|
|
|
|
|
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() |
|
|
|
|
|
|
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
|
|
|
|
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.