Controller::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
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
    /**
69
     * Returns a json response that contains the specified resource
70
     * passed through fractal and optionally a transformer.
71
     *
72
     * @param $item
73
     * @param null $callback
74
     * @param null $resourceKey
75
     * @return \Illuminate\Http\JsonResponse
76
     */
77
    protected function respondWithItem($item, $callback = null, $resourceKey = null)
78
    {
79
        $rootScope = $this->fractal->item($item, $callback, $resourceKey);
80
81
        return $this->respondWithArray($rootScope->toArray());
82
    }
83
84
    /**
85
     * Returns a json response that indicates the resource was successfully created also
86
     * returns the resource passed through fractal and optionally a transformer.
87
     *
88
     * @param $item
89
     * @param null $callback
90
     * @param null $resourceKey
91
     * @return \Illuminate\Http\JsonResponse
92
     */
93
    protected function respondWithItemCreated($item, $callback = null, $resourceKey = null)
94
    {
95
        $this->setStatusCode(201);
96
        $rootScope = $this->fractal->item($item, $callback, $resourceKey);
97
98
        return $this->respondWithArray($rootScope->toArray());
99
    }
100
101
    /**
102
     * Returns a json response that contains the specified collection
103
     * passed through fractal and optionally a transformer.
104
     *
105
     * @param $collection
106
     * @param $callback
107
     * @param null $resourceKey
108
     * @return \Illuminate\Http\JsonResponse
109
     */
110
    protected function respondWithCollection($collection, $callback, $resourceKey = null)
111
    {
112
        $rootScope = $this->fractal->collection($collection, $callback, $resourceKey);
113
114
        return $this->respondWithArray($rootScope->toArray());
115
    }
116
117
    /**
118
     * Returns a json response that contains the specified paginated collection
119
     * passed through fractal and optionally a transformer.
120
     *
121
     * @param $builder
122
     * @param $callback
123
     * @param int $perPage
124
     * @param null $resourceKey
125
     * @return \Illuminate\Http\JsonResponse
126
     */
127
    protected function respondWithPaginatedCollection($builder, $callback, $perPage = 10, $resourceKey = null)
128
    {
129
        $paginator = $builder->paginate($perPage);
130
        $paginator->appends($this->getQueryParameters());
131
132
        $rootScope = $this->fractal
133
            ->collection($paginator->getCollection(), $callback, $resourceKey)
134
            ->paginateWith(new IlluminatePaginatorAdapter($paginator));
135
136
        return $this->respondWithArray($rootScope->toArray());
137
    }
138
139
    /**
140
     * Returns an array of Query Parameters, not including pagination.
141
     *
142
     * @return array
143
     */
144
    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...
145
    {
146
        return array_diff_key($_GET, array_flip(['page']));
147
    }
148
149
    /**
150
     * Returns a json response that contains the specified array,
151
     * the current status code and optional headers.
152
     *
153
     * @param array $array
154
     * @param array $headers
155
     * @return \Illuminate\Http\JsonResponse
156
     */
157
    protected function respondWithArray(array $array, array $headers = [])
158
    {
159
        return response()->json($array, $this->statusCode, $headers);
160
    }
161
162
    /**
163
     * Returns a response that indicates success but no content returned.
164
     *
165
     * @return \Illuminate\Http\Response
166
     */
167
    protected function respondWithNoContent()
168
    {
169
        return response()->json('', 204);
170
    }
171
}
172