Completed
Push — master ( 11ab51...fc8810 )
by Alexander
04:57
created
src/ResponderServiceProvider.php 1 patch
Spacing   +31 added lines, -31 removed lines patch added patch discarded remove patch
@@ -43,18 +43,18 @@  discard block
 block discarded – undo
43 43
      */
44 44
     public function boot()
45 45
     {
46
-        if ($this->app instanceof Laravel && $this->app->runningInConsole()) {
46
+        if ( $this->app instanceof Laravel && $this->app->runningInConsole() ) {
47 47
             $this->bootLaravelApplication();
48 48
 
49
-        } elseif ($this->app instanceof Lumen) {
49
+        } elseif ( $this->app instanceof Lumen ) {
50 50
             $this->bootLumenApplication();
51 51
         }
52 52
 
53
-        $this->mergeConfigFrom(__DIR__ . '/../resources/config/responder.php', 'responder');
53
+        $this->mergeConfigFrom( __DIR__ . '/../resources/config/responder.php', 'responder' );
54 54
 
55
-        $this->commands([
55
+        $this->commands( [
56 56
             MakeTransformer::class
57
-        ]);
57
+        ] );
58 58
 
59 59
         include __DIR__ . '/helpers.php';
60 60
     }
@@ -69,8 +69,8 @@  discard block
 block discarded – undo
69 69
         $this->registerFractal();
70 70
         $this->registerResponseBuilders();
71 71
 
72
-        $this->app->bind(Responder::class, function ($app) {
73
-            return new Responder($app[SuccessResponseBuilder::class], $app[ErrorResponseBuilder::class]);
72
+        $this->app->bind( Responder::class, function ( $app ) {
73
+            return new Responder( $app[ SuccessResponseBuilder::class ], $app[ ErrorResponseBuilder::class ] );
74 74
         });
75 75
 
76 76
         $this->registerAliases();
@@ -83,7 +83,7 @@  discard block
 block discarded – undo
83 83
      */
84 84
     public function provides()
85 85
     {
86
-        return ['responder', 'responder.success', 'responder.error', 'responder.manager', 'responder.serializer'];
86
+        return [ 'responder', 'responder.success', 'responder.error', 'responder.manager', 'responder.serializer' ];
87 87
     }
88 88
 
89 89
     /**
@@ -94,17 +94,17 @@  discard block
 block discarded – undo
94 94
      */
95 95
     protected function registerFractal()
96 96
     {
97
-        $this->app->bind(SerializerAbstract::class, function ($app) {
98
-            $serializer = $app->config->get('responder.serializer');
97
+        $this->app->bind( SerializerAbstract::class, function ( $app ) {
98
+            $serializer = $app->config->get( 'responder.serializer' );
99 99
 
100 100
             return new $serializer;
101 101
         });
102 102
 
103
-        $this->app->bind(Manager::class, function ($app) {
104
-            return (new Manager())->setSerializer($app[SerializerAbstract::class]);
103
+        $this->app->bind( Manager::class, function ( $app ) {
104
+            return (new Manager())->setSerializer( $app[ SerializerAbstract::class ] );
105 105
         });
106 106
 
107
-        $this->app->bind(ResourceFactory::class, function () {
107
+        $this->app->bind( ResourceFactory::class, function () {
108 108
             return new ResourceFactory();
109 109
         });
110 110
     }
@@ -116,16 +116,16 @@  discard block
 block discarded – undo
116 116
      */
117 117
     protected function registerResponseBuilders()
118 118
     {
119
-        $this->app->bind(SuccessResponseBuilder::class, function ($app) {
120
-            $builder = new SuccessResponseBuilder($app[ResponseFactory::class], $app[ResourceFactory::class], $app[Manager::class]);
119
+        $this->app->bind( SuccessResponseBuilder::class, function ( $app ) {
120
+            $builder = new SuccessResponseBuilder( $app[ ResponseFactory::class ], $app[ ResourceFactory::class ], $app[ Manager::class ] );
121 121
 
122
-            return $builder->setIncludeStatusCode($app->config->get('responder.include_status_code'));
122
+            return $builder->setIncludeStatusCode( $app->config->get( 'responder.include_status_code' ) );
123 123
         });
124 124
 
125
-        $this->app->bind(ErrorResponseBuilder::class, function ($app) {
126
-            $builder = new ErrorResponseBuilder($app[ResponseFactory::class], $app['translator']);
125
+        $this->app->bind( ErrorResponseBuilder::class, function ( $app ) {
126
+            $builder = new ErrorResponseBuilder( $app[ ResponseFactory::class ], $app[ 'translator' ] );
127 127
 
128
-            return $builder->setIncludeStatusCode($app->config->get('responder.include_status_code'));
128
+            return $builder->setIncludeStatusCode( $app->config->get( 'responder.include_status_code' ) );
129 129
         });
130 130
     }
131 131
 
@@ -136,11 +136,11 @@  discard block
 block discarded – undo
136 136
      */
137 137
     protected function registerAliases()
138 138
     {
139
-        $this->app->alias(Responder::class, 'responder');
140
-        $this->app->alias(SuccessResponseBuilder::class, 'responder.success');
141
-        $this->app->alias(ErrorResponseBuilder::class, 'responder.error');
142
-        $this->app->alias(Manager::class, 'responder.manager');
143
-        $this->app->alias(Manager::class, 'responder.serializer');
139
+        $this->app->alias( Responder::class, 'responder' );
140
+        $this->app->alias( SuccessResponseBuilder::class, 'responder.success' );
141
+        $this->app->alias( ErrorResponseBuilder::class, 'responder.error' );
142
+        $this->app->alias( Manager::class, 'responder.manager' );
143
+        $this->app->alias( Manager::class, 'responder.serializer' );
144 144
     }
145 145
 
146 146
     /**
@@ -150,13 +150,13 @@  discard block
 block discarded – undo
150 150
      */
151 151
     protected function bootLaravelApplication()
152 152
     {
153
-        $this->publishes([
154
-            __DIR__ . '/../resources/config/responder.php' => config_path('responder.php')
155
-        ], 'config');
153
+        $this->publishes( [
154
+            __DIR__ . '/../resources/config/responder.php' => config_path( 'responder.php' )
155
+        ], 'config' );
156 156
 
157
-        $this->publishes([
158
-            __DIR__ . '/../resources/lang/en/errors.php' => resource_path('lang/en/errors.php')
159
-        ], 'lang');
157
+        $this->publishes( [
158
+            __DIR__ . '/../resources/lang/en/errors.php' => resource_path( 'lang/en/errors.php' )
159
+        ], 'lang' );
160 160
     }
161 161
 
162 162
     /**
@@ -166,6 +166,6 @@  discard block
 block discarded – undo
166 166
      */
167 167
     protected function bootLumenApplication()
168 168
     {
169
-        $this->app->configure('responder');
169
+        $this->app->configure( 'responder' );
170 170
     }
171 171
 }
172 172
\ No newline at end of file
Please login to merge, or discard this patch.
src/Http/SuccessResponseBuilder.php 1 patch
Spacing   +48 added lines, -48 removed lines patch added patch discarded remove patch
@@ -39,7 +39,7 @@  discard block
 block discarded – undo
39 39
      *
40 40
      * @var array
41 41
      */
42
-    protected $meta = [];
42
+    protected $meta = [ ];
43 43
 
44 44
     /**
45 45
      * The Fractal resource instance containing the data and transformer.
@@ -69,13 +69,13 @@  discard block
 block discarded – undo
69 69
      * @param \Flugg\Responder\ResourceFactory              $resourceFactory
70 70
      * @param \League\Fractal\Manager                       $manager
71 71
      */
72
-    public function __construct(ResponseFactory $responseFactory, ResourceFactory $resourceFactory, Manager $manager)
72
+    public function __construct( ResponseFactory $responseFactory, ResourceFactory $resourceFactory, Manager $manager )
73 73
     {
74 74
         $this->resourceFactory = $resourceFactory;
75 75
         $this->manager = $manager;
76 76
         $this->resource = $this->resourceFactory->make();
77 77
 
78
-        parent::__construct($responseFactory);
78
+        parent::__construct( $responseFactory );
79 79
     }
80 80
 
81 81
     /**
@@ -84,9 +84,9 @@  discard block
 block discarded – undo
84 84
      * @param  array $data
85 85
      * @return self
86 86
      */
87
-    public function addMeta(array $data):SuccessResponseBuilder
87
+    public function addMeta( array $data ):SuccessResponseBuilder
88 88
     {
89
-        $this->meta = array_merge($this->meta, $data);
89
+        $this->meta = array_merge( $this->meta, $data );
90 90
 
91 91
         return $this;
92 92
     }
@@ -97,9 +97,9 @@  discard block
 block discarded – undo
97 97
      * @param  \League\Fractal\Serializer\SerializerAbstract|string $serializer
98 98
      * @return self
99 99
      */
100
-    public function serializer($serializer):SuccessResponseBuilder
100
+    public function serializer( $serializer ):SuccessResponseBuilder
101 101
     {
102
-        $this->manager->setSerializer($this->resolveSerializer($serializer));
102
+        $this->manager->setSerializer( $this->resolveSerializer( $serializer ) );
103 103
 
104 104
         return $this;
105 105
     }
@@ -111,13 +111,13 @@  discard block
 block discarded – undo
111 111
      * @return self
112 112
      * @throws \InvalidArgumentException
113 113
      */
114
-    public function setStatus(int $statusCode):ResponseBuilder
114
+    public function setStatus( int $statusCode ):ResponseBuilder
115 115
     {
116
-        if ($statusCode < 100 || $statusCode >= 400) {
117
-            throw new InvalidArgumentException("{$statusCode} is not a valid success HTTP status code.");
116
+        if ( $statusCode < 100 || $statusCode >= 400 ) {
117
+            throw new InvalidArgumentException( "{$statusCode} is not a valid success HTTP status code." );
118 118
         }
119 119
 
120
-        return parent::setStatus($statusCode);
120
+        return parent::setStatus( $statusCode );
121 121
     }
122 122
 
123 123
     /**
@@ -129,17 +129,17 @@  discard block
 block discarded – undo
129 129
      * @param  string|null          $resourceKey
130 130
      * @return self
131 131
      */
132
-    public function transform($data = null, $transformer = null, string $resourceKey = null):SuccessResponseBuilder
132
+    public function transform( $data = null, $transformer = null, string $resourceKey = null ):SuccessResponseBuilder
133 133
     {
134
-        $resource = $this->resourceFactory->make($data);
134
+        $resource = $this->resourceFactory->make( $data );
135 135
 
136
-        if (! is_null($resource->getData())) {
137
-            $model = $this->resolveModel($resource->getData());
138
-            $transformer = $this->resolveTransformer($model, $transformer);
139
-            $resourceKey = $this->resolveResourceKey($model, $resourceKey);
136
+        if ( ! is_null( $resource->getData() ) ) {
137
+            $model = $this->resolveModel( $resource->getData() );
138
+            $transformer = $this->resolveTransformer( $model, $transformer );
139
+            $resourceKey = $this->resolveResourceKey( $model, $resourceKey );
140 140
         }
141 141
 
142
-        $this->resource = $resource->setTransformer($transformer)->setResourceKey($resourceKey);
142
+        $this->resource = $resource->setTransformer( $transformer )->setResourceKey( $resourceKey );
143 143
 
144 144
         return $this;
145 145
     }
@@ -151,7 +151,7 @@  discard block
 block discarded – undo
151 151
      */
152 152
     public function toArray():array
153 153
     {
154
-        return $this->serialize($this->getResource());
154
+        return $this->serialize( $this->getResource() );
155 155
     }
156 156
 
157 157
     /**
@@ -161,7 +161,7 @@  discard block
 block discarded – undo
161 161
      */
162 162
     public function getResource():ResourceInterface
163 163
     {
164
-        return $this->resource->setMeta($this->meta);
164
+        return $this->resource->setMeta( $this->meta );
165 165
     }
166 166
 
167 167
     /**
@@ -181,13 +181,13 @@  discard block
 block discarded – undo
181 181
      * @return \League\Fractal\Serializer\SerializerAbstract
182 182
      * @throws \Flugg\Responder\Exceptions\InvalidSerializerException
183 183
      */
184
-    protected function resolveSerializer($serializer):SerializerAbstract
184
+    protected function resolveSerializer( $serializer ):SerializerAbstract
185 185
     {
186
-        if (is_string($serializer)) {
186
+        if ( is_string( $serializer ) ) {
187 187
             $serializer = new $serializer;
188 188
         }
189 189
 
190
-        if (! $serializer instanceof SerializerAbstract) {
190
+        if ( ! $serializer instanceof SerializerAbstract ) {
191 191
             throw new InvalidSerializerException();
192 192
         }
193 193
 
@@ -201,15 +201,15 @@  discard block
 block discarded – undo
201 201
      * @return \Illuminate\Database\Eloquent\Model
202 202
      * @throws \InvalidArgumentException
203 203
      */
204
-    protected function resolveModel($data):Model
204
+    protected function resolveModel( $data ):Model
205 205
     {
206
-        if ($data instanceof Model) {
206
+        if ( $data instanceof Model ) {
207 207
             return $data;
208 208
         }
209 209
 
210
-        $model = $data[0];
211
-        if (! $model instanceof Model) {
212
-            throw new InvalidArgumentException('You can only transform data containing Eloquent models.');
210
+        $model = $data[ 0 ];
211
+        if ( ! $model instanceof Model ) {
212
+            throw new InvalidArgumentException( 'You can only transform data containing Eloquent models.' );
213 213
         }
214 214
 
215 215
         return $model;
@@ -223,20 +223,20 @@  discard block
 block discarded – undo
223 223
      * @return \Flugg\Responder\Transformer|callable
224 224
      * @throws \InvalidTransformerException
225 225
      */
226
-    protected function resolveTransformer(Model $model, $transformer = null)
226
+    protected function resolveTransformer( Model $model, $transformer = null )
227 227
     {
228
-        if (is_null($transformer)) {
229
-            $transformer = $this->resolveTransformerFromModel($model);
228
+        if ( is_null( $transformer ) ) {
229
+            $transformer = $this->resolveTransformerFromModel( $model );
230 230
         }
231 231
 
232
-        if (is_string($transformer)) {
232
+        if ( is_string( $transformer ) ) {
233 233
             $transformer = new $transformer;
234 234
         }
235 235
 
236
-        if ($transformer instanceof Transformer) {
237
-            $this->setRelations($transformer, $model);
238
-        } elseif (! is_callable($transformer)) {
239
-            throw new InvalidTransformerException($model);
236
+        if ( $transformer instanceof Transformer ) {
237
+            $this->setRelations( $transformer, $model );
238
+        } elseif ( ! is_callable( $transformer ) ) {
239
+            throw new InvalidTransformerException( $model );
240 240
         }
241 241
 
242 242
         return $transformer;
@@ -249,9 +249,9 @@  discard block
 block discarded – undo
249 249
      * @param  \Illuminate\Database\ELoquent\Model $model
250 250
      * @return \Flugg\Responder\Transformer|callable
251 251
      */
252
-    protected function resolveTransformerFromModel(Model $model)
252
+    protected function resolveTransformerFromModel( Model $model )
253 253
     {
254
-        if (! $model instanceof Transformable) {
254
+        if ( ! $model instanceof Transformable ) {
255 255
             return return function () use ($model) {
256 256
                 return $model->toArray();
257 257
             };
@@ -267,11 +267,11 @@  discard block
 block discarded – undo
267 267
      * @param  \Illuminate\Database\Eloquent\Model $model
268 268
      * @return void
269 269
      */
270
-    protected function setRelations(Transformer $transformer, Model $model)
270
+    protected function setRelations( Transformer $transformer, Model $model )
271 271
     {
272
-        $transformer->setRelations($this->resolveRelations($model));
272
+        $transformer->setRelations( $this->resolveRelations( $model ) );
273 273
 
274
-        $this->manager->parseIncludes($transformer->getRelations());
274
+        $this->manager->parseIncludes( $transformer->getRelations() );
275 275
     }
276 276
 
277 277
     /**
@@ -280,9 +280,9 @@  discard block
 block discarded – undo
280 280
      * @param  \Illuminate\Database\Eloquent\Model $model
281 281
      * @return array
282 282
      */
283
-    protected function resolveRelations(Model $model):array
283
+    protected function resolveRelations( Model $model ):array
284 284
     {
285
-        return array_keys($model->getRelations());
285
+        return array_keys( $model->getRelations() );
286 286
     }
287 287
 
288 288
     /**
@@ -292,13 +292,13 @@  discard block
 block discarded – undo
292 292
      * @param  string|null                         $resourceKey
293 293
      * @return string
294 294
      */
295
-    protected function resolveResourceKey(Model $model, string $resourceKey = null):string
295
+    protected function resolveResourceKey( Model $model, string $resourceKey = null ):string
296 296
     {
297
-        if (! is_null($resourceKey)) {
297
+        if ( ! is_null( $resourceKey ) ) {
298 298
             return $resourceKey;
299 299
         }
300 300
 
301
-        if (method_exists($model, 'getResourceKey')) {
301
+        if ( method_exists( $model, 'getResourceKey' ) ) {
302 302
             return $model->getResourceKey();
303 303
         }
304 304
 
@@ -311,8 +311,8 @@  discard block
 block discarded – undo
311 311
      * @param  ResourceInterface $resource
312 312
      * @return array
313 313
      */
314
-    protected function serialize(ResourceInterface $resource):array
314
+    protected function serialize( ResourceInterface $resource ):array
315 315
     {
316
-        return $this->manager->createData($resource)->toArray();
316
+        return $this->manager->createData( $resource )->toArray();
317 317
     }
318 318
 }
319 319
\ No newline at end of file
Please login to merge, or discard this patch.
src/Traits/HandlesApiErrors.php 1 patch
Spacing   +18 added lines, -18 removed lines patch added patch discarded remove patch
@@ -31,11 +31,11 @@  discard block
 block discarded – undo
31 31
      * @param  Exception $exception
32 32
      * @return void
33 33
      */
34
-    protected function transformException(Exception $exception)
34
+    protected function transformException( Exception $exception )
35 35
     {
36
-        $this->transformAuthException($exception);
37
-        $this->transformEloquentException($exception);
38
-        $this->transformValidationException($exception);
36
+        $this->transformAuthException( $exception );
37
+        $this->transformEloquentException( $exception );
38
+        $this->transformValidationException( $exception );
39 39
     }
40 40
 
41 41
     /**
@@ -46,13 +46,13 @@  discard block
 block discarded – undo
46 46
      * @throws UnauthenticatedException
47 47
      * @throws UnauthorizedException
48 48
      */
49
-    protected function transformAuthException(Exception $exception)
49
+    protected function transformAuthException( Exception $exception )
50 50
     {
51
-        if ($exception instanceof AuthenticationException) {
51
+        if ( $exception instanceof AuthenticationException ) {
52 52
             throw new UnauthenticatedException();
53 53
         }
54 54
 
55
-        if ($exception instanceof AuthorizationException) {
55
+        if ( $exception instanceof AuthorizationException ) {
56 56
             throw new UnauthorizedException();
57 57
         }
58 58
     }
@@ -65,13 +65,13 @@  discard block
 block discarded – undo
65 65
      * @throws ResourceNotFoundException
66 66
      * @throws RelationNotFoundException
67 67
      */
68
-    protected function transformEloquentException(Exception $exception)
68
+    protected function transformEloquentException( Exception $exception )
69 69
     {
70
-        if ($exception instanceof ModelNotFoundException) {
70
+        if ( $exception instanceof ModelNotFoundException ) {
71 71
             throw new ResourceNotFoundException();
72 72
         }
73 73
 
74
-        if ($exception instanceof RelationNotFoundException) {
74
+        if ( $exception instanceof RelationNotFoundException ) {
75 75
             throw new RelationNotFoundException();
76 76
         }
77 77
     }
@@ -83,10 +83,10 @@  discard block
 block discarded – undo
83 83
      * @return void
84 84
      * @throws ValidationFailedException
85 85
      */
86
-    protected function transformValidationException(Exception $exception)
86
+    protected function transformValidationException( Exception $exception )
87 87
     {
88
-        if ($exception instanceof ValidationException) {
89
-            throw new ValidationFailedException($exception->validator);
88
+        if ( $exception instanceof ValidationException ) {
89
+            throw new ValidationFailedException( $exception->validator );
90 90
         }
91 91
     }
92 92
 
@@ -96,11 +96,11 @@  discard block
 block discarded – undo
96 96
      * @param  ApiException $exception
97 97
      * @return JsonResponse
98 98
      */
99
-    protected function renderApiError(ApiException $exception):JsonResponse
99
+    protected function renderApiError( ApiException $exception ):JsonResponse
100 100
     {
101
-        return app('responder.error')
102
-            ->setError($exception->getErrorCode(), $exception->getMessage())
103
-            ->addData($exception->getData())
104
-            ->respond($exception->getStatusCode());
101
+        return app( 'responder.error' )
102
+            ->setError( $exception->getErrorCode(), $exception->getMessage() )
103
+            ->addData( $exception->getData() )
104
+            ->respond( $exception->getStatusCode() );
105 105
     }
106 106
 }
107 107
\ No newline at end of file
Please login to merge, or discard this patch.