TagController::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
6
use App\Transformers\TagTransformer;
7
use App\Tag;
8
use App\Task;
9
use Illuminate\Http\Request;
10
use Illuminate\Http\Response as IlluminateResponse;
11
use Illuminate\Support\Facades\Input;
12
13
class TagController extends ApiController
14
{
15
    /**
16
     * @var TagTransformer
17
     */
18
    protected $tagTransformer;
19
    /**
20
     * TagController constructor.
21
     * @param $tagTransformer
22
     */
23
    public function __construct(TagTransformer $tagTransformer)
24
    {
25
        $this->tagTransformer = $tagTransformer;
26
    }
27
28
    /**
29
     * Display a listing of the resource.
30
     * @param null $taskId
31
     * @return \Illuminate\Http\Response
32
     */
33
    public function index($taskId = null)
34
    {
35
        $tag = $this->getTags($taskId);
36
        return $this->respond($this->tagTransformer->transformCollection($tag->all()));
37
    }
38
39
    /**
40
     * Show the form for creating a new resource.
41
     *
42
     * @return \Illuminate\Http\Response
43
     */
44
    public function create()
45
    {
46
        //
47
    }
48
49
    /**
50
     * Store a newly created resource in storage.
51
     *  @return \Illuminate\Http\Response
52
     */
53
    public function store()
54
    {
55
        if (!Input::get('title'))
56
        {
57
            return $this->setStatusCode(IlluminateResponse::HTTP_UNPROCESSABLE_ENTITY)
58
                ->respondWithError('Parameters failed validation for a task.');
59
        }
60
61
        Tag::create(Input::all());
62
63
        return $this->respondCreated('Task successfully created.');
64
    }
65
66
    /**
67
     * Display the specified resource.
68
     *
69
     * @param  int $id
70
     * @return \Illuminate\Http\Response
71
     */
72 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...
73
    {
74
        $tag = Tag::find($id);
75
76
        if (!$tag) {
77
            return $this->respondNotFound('Tag does not exsist');
78
        }
79
80
        return $this->respond([
81
            'data' => $this->tagTransformer->transform($tag)
82
        ]);
83
    }
84
85
    /**
86
     * Show the form for editing the specified resource.
87
     *
88
     * @param  int $id
89
     * @return \Illuminate\Http\Response
90
     */
91
    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...
92
    {
93
        //
94
    }
95
96
    /**
97
     * Update the specified resource in storage.
98
     *
99
     * @param  \Illuminate\Http\Request $request
100
     * @param  int $id
101
     * @return \Illuminate\Http\Response
102
     */
103
    public function update(Request $request, $id)
104
    {
105
        $tag = Tag::find($id);
106
        if (!$tag)
107
        {
108
            return $this->respondNotFound('Tag does not exist!');
109
        }
110
        $tag->title = $request->title;
111
        $tag->save();
112
    }
113
114
    /**
115
     * Remove the specified resource from storage.
116
     *
117
     * @param  int $id
118
     * @return \Illuminate\Http\Response
119
     */
120
    public function destroy($id)
121
    {
122
        Tag::destroy($id);
123
    }
124
125
    /**
126
     * @param $taskId
127
     * @return \Illuminate\Database\Eloquent\Collection|static[]
128
     */
129
    public function getTags($taskId)
130
    {
131
        return $taskId ? Task::findOrFail($taskId)->tags : Tag::all();
132
    }
133
}