Completed
Pull Request — master (#5)
by Zach
01:49
created

Controller   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 6
dl 0
loc 174
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getStatusCode() 0 4 1
A setStatusCode() 0 6 1
A __construct() 0 6 1
A parseIncludes() 0 12 3
A prepareBuilder() 0 8 2
A respondWithItem() 0 10 1
A respondWithItemCreated() 0 7 1
A respondWithCollection() 0 6 1
A respondWithPaginatedCollection() 0 13 1
A getQueryParameters() 0 4 1
A respondWithArray() 0 4 1
A respondWithNoContent() 0 4 1
1
<?php
2
3
namespace NavJobs\Transmit;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Support\Facades\App;
7
use Illuminate\Support\Facades\Input;
8
use NavJobs\Transmit\Traits\ErrorResponsesTrait;
9
use NavJobs\Transmit\Traits\QueryHelperTrait;
10
use Illuminate\Routing\Controller as BaseController;
11
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
12
13
abstract class Controller extends BaseController
14
{
15
    use QueryHelperTrait, ErrorResponsesTrait;
16
17
    protected $statusCode = 200;
18
    protected $fractal;
19
20
    public function __construct()
21
    {
22
        $this->fractal = App::make(Fractal::class);
23
24
        $this->parseIncludes();
25
    }
26
27
    /**
28
     * Parses includes from either the header or query string.
29
     *
30
     * @return mixed
31
     */
32
    protected function parseIncludes()
33
    {
34
        if (Input::header('include')) {
35
            return $this->fractal->parseIncludes(Input::header('include'));
36
        }
37
38
        if (Input::get('include')) {
39
            return $this->fractal->parseIncludes(Input::get('include'));
40
        }
41
42
        return null;
43
    }
44
45
    /**
46
     * Returns the current status code.
47
     *
48
     * @return int
49
     */
50
    protected function getStatusCode()
51
    {
52
        return $this->statusCode;
53
    }
54
55
    /**
56
     * Sets the current status code.
57
     *
58
     * @param $statusCode
59
     * @return $this
60
     */
61
    protected function setStatusCode($statusCode)
62
    {
63
        $this->statusCode = $statusCode;
64
65
        return $this;
66
    }
67
68
    private function prepareBuilder($builder)
69
    {
70
        $model = $builder ?: $this->model;
0 ignored issues
show
Bug introduced by
The property model does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Unused Code introduced by
$model is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
71
72
        $includes = $this->transformer->getEagerLoads($this->fractal->getRequestedIncludes());
0 ignored issues
show
Bug introduced by
The property transformer does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
73
        $includedItems = $this->eagerLoadIncludes($this->model, $includes);
74
        return $this->applyParameters($includedItems, request()->query);
75
    }
76
77
    /**
78
     * Returns a json response that contains the specified resource
79
     * passed through fractal and optionally a transformer.
80
     *
81
     * @param $item
82
     * @param null $callback
83
     * @param null $resourceKey
84
     * @return \Illuminate\Http\JsonResponse
85
     */
86
    protected function respondWithItem($builder, $callback, $resourceKey = null)
87
    {
88
        $builder = $this->prepareBuilder($builder);
89
90
        $item = $callback($builder);
91
92
        $rootScope = $this->fractal->item($item, $this->transformer, $resourceKey);
93
94
        return $this->respondWithArray($rootScope->toArray());
95
    }
96
97
    /**
98
     * Returns a json response that indicates the resource was successfully created also
99
     * returns the resource passed through fractal and optionally a transformer.
100
     *
101
     * @param $item
102
     * @param null $callback
103
     * @param null $resourceKey
104
     * @return \Illuminate\Http\JsonResponse
105
     */
106
    protected function respondWithItemCreated($item, $callback = null, $resourceKey = null)
107
    {
108
        $this->setStatusCode(201);
109
        $rootScope = $this->fractal->item($item, $callback, $resourceKey);
110
111
        return $this->respondWithArray($rootScope->toArray());
112
    }
113
114
    /**
115
     * Returns a json response that contains the specified collection
116
     * passed through fractal and optionally a transformer.
117
     *
118
     * @param $collection
119
     * @param $callback
120
     * @param null $resourceKey
121
     * @return \Illuminate\Http\JsonResponse
122
     */
123
    protected function respondWithCollection($collection, $callback, $resourceKey = null)
124
    {
125
        $rootScope = $this->fractal->collection($collection, $callback, $resourceKey);
126
127
        return $this->respondWithArray($rootScope->toArray());
128
    }
129
130
    /**
131
     * Returns a json response that contains the specified paginated collection
132
     * passed through fractal and optionally a transformer.
133
     *
134
     * @param $builder
135
     * @param $callback
136
     * @param int $perPage
137
     * @param null $resourceKey
138
     * @return \Illuminate\Http\JsonResponse
139
     */
140
    protected function respondWithPaginatedCollection($builder = null, $perPage = 10, $resourceKey = null)
141
    {
142
        $builder = $this->prepareBuilder($builder);
143
144
        $paginator = $builder->paginate($perPage);
145
        $paginator->appends($this->getQueryParameters());
146
147
        $rootScope = $this->fractal
148
            ->collection($paginator->getCollection(), $this->transformer, $resourceKey)
149
            ->paginateWith(new IlluminatePaginatorAdapter($paginator));
150
151
        return $this->respondWithArray($rootScope->toArray());
152
    }
153
154
    /**
155
     * Returns an array of Query Parameters, not including pagination.
156
     *
157
     * @return array
158
     */
159
    protected function getQueryParameters()
0 ignored issues
show
Coding Style introduced by
getQueryParameters uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
160
    {
161
        return array_diff_key($_GET, array_flip(['page']));
162
    }
163
164
    /**
165
     * Returns a json response that contains the specified array,
166
     * the current status code and optional headers.
167
     *
168
     * @param array $array
169
     * @param array $headers
170
     * @return \Illuminate\Http\JsonResponse
171
     */
172
    protected function respondWithArray(array $array, array $headers = [])
173
    {
174
        return response()->json($array, $this->statusCode, $headers);
175
    }
176
177
    /**
178
     * Returns a response that indicates success but no content returned.
179
     *
180
     * @return \Illuminate\Http\Response
181
     */
182
    protected function respondWithNoContent()
183
    {
184
        return response()->make('', 204);
185
    }
186
}
187