Completed
Push — development ( 914a5c...ed19c7 )
by Tim
22:29 queued 11:14
created

ApiDepartmentsController::destroy()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 15
rs 9.4285
cc 2
eloc 10
nc 2
nop 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Departments;
6
use App\Http\Requests;
7
use League\Fractal\Manager;
8
use Illuminate\Http\Request;
9
use App\Http\Controllers\Controller;
10
use League\Fractal\Pagination\Cursor;
11
use App\Http\TransformersApi\DepartmentOutput;
12
use League\Fractal\Resource\Collection;
13
use League\Fractal\Resource\Item;
14
15
/**
16
 * Class ApiDepartmentsController
17
 * @package App\Http\Controllers
18
 */
19
class ApiDepartmentsController extends Controller
20
{
21
    private $transformer;
22
    private $fractal;
23
24
    /**
25
     * ApiDepartmentsController constructor.
26
     */
27
    public function __construct()
28
    {
29
        $this->transformer = new DepartmentOutput();
30
        $this->fractal     = new Manager();
31
    }
32
33
    /**
34
     * Display a listing of the resource.
35
     *
36
     * @TODO   Needs test
37
     * @TODO   Needs documentation
38
     * @param  \Illuminate\Http\Request  $request
39
     * @return \Illuminate\Http\Response
40
     */
41
    public function index(Request $request)
42
    {
43
        $currentCursor  = $request->get('cursor', null);
44
        $previousCursor = $request->get('previous', null);
45
        $limit          = $request->get('limit', 10);
46
47
        if ($currentCursor) {
48
            $departments = Departments::with('managers')->where('id', '>', $currentCursor)->take($limit)->get();
49
        } else {
50
            $departments = Departments::with('managers')->take($limit)->get();
51
        }
52
53
        $newCursor = $departments->last()->id;
54
        $cursor    = new Cursor($currentCursor, $previousCursor, $newCursor, $departments->count());
55
        $resource  = new Collection($departments, $this->transformer);
56
57
        $resource->setCursor($cursor);
58
        $outputBody = $this->fractal->createData($resource)->toJson();
59
60
        return response($outputBody, 200)
0 ignored issues
show
Bug introduced by
The method header() does not exist on Symfony\Component\HttpFoundation\Response. Did you maybe mean sendHeaders()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
61
            ->header('Content-Type', 'application/json');
62
    }
63
64
    /**
65
     * Display the specified resource.
66
     *
67
     * @TODO   needs tests
68
     * @TODO   needs documentation
69
     * @param  int  $id
70
     * @return \Illuminate\Http\Response
71
     */
72
    public function show($id)
73
    {
74
        $departments = Departments::find($id);
75
76
        if (count($departments) > 0) {
77
            $resource = new Item($departments, $this->transformer);
78
            $outputBody = $this->fractal->createData($resource)->toJson();
79
        } else {
80
            $outputBody = [];
81
            $outputBody['message'] = 'Could not found a department with that ID.';
82
        }
83
84
        return response($outputBody, 200)
0 ignored issues
show
Bug introduced by
It seems like $outputBody defined by array() on line 80 can also be of type array<string,string,{"message":"string"}>; however, response() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Bug introduced by
The method header() does not exist on Symfony\Component\HttpFoundation\Response. Did you maybe mean sendHeaders()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
85
            ->header('Content-Type', 'application/json');
86
    }
87
88
    /**
89
     * Store a newly created resource in storage.
90
     *
91
     * @TODO   needs test
92
     * @TODO   Needs documentation
93
     * @param  Requests\DepartmentsValidator $input the input bag from the request.
94
     * @return \Illuminate\Http\Response
95
     */
96 View Code Duplication
    public function store(Requests\DepartmentsValidator $input)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
97
    {
98
        Departments::create($input->all());
99
100
        $output = [];
101
        $output['message'] = 'Department created';
102
        $output['data']    = $input->all();
103
104
        return response($output, 201)
0 ignored issues
show
Documentation introduced by
$output is of type array<string,string|array,{"data":"array"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug introduced by
The method header() does not exist on Symfony\Component\HttpFoundation\Response. Did you maybe mean sendHeaders()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
105
            ->header('Content-Type', 'application/json');
106
    }
107
108
    /**
109
     * Update the specified resource in storage.
110
     *
111
     * @TODO   Needs documentation
112
     * @TODO   Needs tests
113
     * @param  Requests\DepartmentsValidator $input the input bag from the request.
114
     * @param  \Illuminate\Http\Request $request
115
     * @param  int $id
116
     * @return \Illuminate\Http\Response
117
     */
118 View Code Duplication
    public function update(Requests\DepartmentsValidator $input, Request $request, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
119
    {
120
        Departments::find($id)->update($input->all());
121
122
        $output = [];
123
        $output['message']  = 'Department updated';
124
        $output['new_data'] = $input->all();
125
126
        return response($output, 200)
0 ignored issues
show
Documentation introduced by
$output is of type array<string,string|array,{"new_data":"array"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug introduced by
The method header() does not exist on Symfony\Component\HttpFoundation\Response. Did you maybe mean sendHeaders()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
127
            ->header('Content-Type', 'application/json');
128
    }
129
130
    /**
131
     * Remove the specified resource from storage.
132
     *
133
     * @TODO   need tests
134
     * #TODO   needs documentation
135
     * @param  int  $id
136
     * @return \Illuminate\Http\Response
137
     */
138
    public function destroy($id)
139
    {
140
        if (! empty($id)) {
141
            Departments::destroy($id);
142
143
            $data = [];
144
            $data['message'] = 'Department deleted';
145
        } else {
146
            $data = [];
147
            $data['message'] = 'Could not delete the department';
148
        }
149
150
        return response($data, 200)
0 ignored issues
show
Documentation introduced by
$data is of type array<string,string,{"message":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug introduced by
The method header() does not exist on Symfony\Component\HttpFoundation\Response. Did you maybe mean sendHeaders()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
151
            ->header('Content-Type', 'application/json');
152
    }
153
}
154