Completed
Push — master ( 911991...0a9dc2 )
by Yves
07:41
created

TagController::saveTag()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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