TaskController::store()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 10
rs 9.2
cc 4
eloc 6
nc 2
nop 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Task;
6
use App\Transformers\TaskTransformer;
7
use Illuminate\Http\Request;
8
use Illuminate\Http\Response as IlluminateResponse;
9
use Illuminate\Support\Facades\Input;
10
11
class TaskController extends ApiController
12
{
13
    protected $taskTransformer;
14
    /**
15
     * TaskController constructor.
16
     * @param $taskTransformer
17
     */
18
    public function __construct(TaskTransformer $taskTransformer)
19
    {
20
        $this->taskTransformer = $taskTransformer;
21
    }
22
    /**
23
     * Display a listing of the resource.
24
     *
25
     * @return \Illuminate\Http\Response
26
     */
27
    public function index()
28
    {
29
30
        $task = Task::all();
31
        return $this->respond($this->taskTransformer->transformCollection($task->all()));
32
    }
33
    /**
34
     * Show the form for creating a new resource.
35
     *
36
     * @return \Illuminate\Http\Response
37
     */
38
    public function create()
39
    {
40
        //
41
    }
42
    /**
43
     * Store a newly created resource in storage.
44
     *  @return \Illuminate\Http\Response
45
     */
46
    public function store()
47
    {
48
        if (!Input::get('name') or !Input::get('done') or !Input::get('priority'))
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
49
        {
50
            return $this->setStatusCode(IlluminateResponse::HTTP_UNPROCESSABLE_ENTITY)
51
                ->respondWithError('Parameters failed validation for a task.');
52
        }
53
        Task::create(Input::all());
54
        return $this->respondCreated('Task successfully created.');
55
    }
56
    /**
57
     * Display the specified resource.
58
     *
59
     * @param  int $id
60
     * @return \Illuminate\Http\Response
61
     */
62 View Code Duplication
    public function show($id)
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...
63
    {
64
        $task = Task::find($id);
65
        if (!$task) {
66
            return $this->respondNotFound('Task does not exsist');
67
        }
68
        return $this->respond([
69
            'data' => $this->taskTransformer->transform($task)
70
        ]);
71
    }
72
    /**
73
     * Show the form for editing the specified resource.
74
     *
75
     * @param  int $id
76
     * @return \Illuminate\Http\Response
77
     */
78
    public function edit($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id 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...
79
    {
80
        //
81
    }
82
    /**
83
     * Update the specified resource in storage.
84
     *
85
     * @param  \Illuminate\Http\Request $request
86
     * @param  int $id
87
     * @return \Illuminate\Http\Response
88
     */
89
    public function update(Request $request, $id)
90
    {
91
        $task = Task::find($id);
92
        if (!$task)
93
        {
94
            return $this->respondNotFound('Task does not exist!!');
95
        }
96
        $task->name = $request->name;
97
        $task->priority = $request->priority;
98
        $task->done = $request->done;
99
        $task->save();
100
    }
101
    /**
102
     * Remove the specified resource from storage.
103
     *
104
     * @param  int $id
105
     * @return \Illuminate\Http\Response
106
     */
107
    public function destroy($id)
108
    {
109
        Task::destroy($id);
110
    }
111
}
112