1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NavJobs\Transmit; |
4
|
|
|
|
5
|
|
|
use League\Fractal\ParamBag; |
6
|
|
|
use Illuminate\Support\Facades\App; |
7
|
|
|
use NavJobs\Transmit\Traits\QueryHelperTrait; |
8
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
9
|
|
|
|
10
|
|
|
abstract class Gateway |
11
|
|
|
{ |
12
|
|
|
use QueryHelperTrait; |
13
|
|
|
|
14
|
|
|
protected $statusCode = 200; |
15
|
|
|
protected $fractal; |
16
|
|
|
|
17
|
|
|
public function __construct() |
18
|
|
|
{ |
19
|
|
|
$this->fractal = App::make(Fractal::class); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Returns the current status code. |
24
|
|
|
* |
25
|
|
|
* @return int |
26
|
|
|
*/ |
27
|
|
|
protected function getStatusCode() |
28
|
|
|
{ |
29
|
|
|
return $this->statusCode; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Sets the current status code. |
34
|
|
|
* |
35
|
|
|
* @param $statusCode |
36
|
|
|
* @return $this |
37
|
|
|
*/ |
38
|
|
|
protected function setStatusCode($statusCode) |
39
|
|
|
{ |
40
|
|
|
$this->statusCode = $statusCode; |
41
|
|
|
|
42
|
|
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Returns a json response that contains the specified resource |
47
|
|
|
* passed through fractal and optionally a transformer. |
48
|
|
|
* |
49
|
|
|
* @param $item |
50
|
|
|
* @param null $callback |
51
|
|
|
* @param null $resourceKey |
52
|
|
|
* @return \Illuminate\Http\JsonResponse |
53
|
|
|
*/ |
54
|
|
View Code Duplication |
protected function respondWithItem($item, $callback = null, $resourceKey = null) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$rootScope = $this->fractal |
57
|
|
|
->item($item, $callback, $resourceKey); |
58
|
|
|
|
59
|
|
|
return $this->respondWithArray($rootScope->toArray()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Returns a json response that contains the specified collection |
64
|
|
|
* passed through fractal and optionally a transformer. |
65
|
|
|
* |
66
|
|
|
* @param $collection |
67
|
|
|
* @param $callback |
68
|
|
|
* @param null $resourceKey |
69
|
|
|
* @return \Illuminate\Http\JsonResponse |
70
|
|
|
*/ |
71
|
|
|
protected function respondWithCollection($collection, $callback, $resourceKey = null) |
72
|
|
|
{ |
73
|
|
|
$rootScope = $this->fractal->collection($collection, $callback, $resourceKey); |
74
|
|
|
|
75
|
|
|
return $this->respondWithArray($rootScope->toArray()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Parses the incoming json parameters into a ParameterBag object. |
80
|
|
|
* |
81
|
|
|
* @param $parameters |
82
|
|
|
* @return ParameterBag |
83
|
|
|
*/ |
84
|
|
|
protected function parseParameters($parameters) |
85
|
|
|
{ |
86
|
|
|
if (!($parameters instanceof ParameterBag) && !($parameters instanceof ParamBag)) { |
87
|
|
|
$parameters = new ParameterBag(json_decode($parameters, true)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if ($parameters->get('with')) { |
91
|
|
|
$this->fractal->parseIncludes($parameters->get('with')); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $parameters; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns a json response that contains the specified array, |
99
|
|
|
* and the current status code. |
100
|
|
|
* |
101
|
|
|
* @param array $array |
102
|
|
|
* @return \Illuminate\Http\JsonResponse |
103
|
|
|
*/ |
104
|
|
|
protected function respondWithArray(array $array) |
105
|
|
|
{ |
106
|
|
|
return json_encode(array_merge($array, ['status_code' => $this->statusCode])); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Returns a response that indicates a 404 Not Found. |
111
|
|
|
* |
112
|
|
|
* @param string $message |
113
|
|
|
* @return \Illuminate\Http\JsonResponse |
114
|
|
|
*/ |
115
|
|
|
protected function errorNotFound($message = 'Resource Not Found') |
116
|
|
|
{ |
117
|
|
|
return $this->setStatusCode(404)->respondWithError($message); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Returns a response that indicates an an error occurred. |
122
|
|
|
* |
123
|
|
|
* @param $message |
124
|
|
|
* @return \Illuminate\Http\JsonResponse |
125
|
|
|
*/ |
126
|
|
|
protected function respondWithError($message) |
127
|
|
|
{ |
128
|
|
|
return $this->respondWithArray([ |
129
|
|
|
'error' => [ |
130
|
|
|
'status_code' => $this->statusCode, |
131
|
|
|
'message' => $message, |
132
|
|
|
] |
133
|
|
|
]); |
134
|
|
|
} |
135
|
|
|
} |
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.