TagController   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 9
Bugs 0 Features 0
Metric Value
wmc 13
c 9
b 0
f 0
lcom 2
cbo 4
dl 0
loc 122
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A destroy() 0 4 1
A __construct() 0 5 1
A index() 0 6 1
A create() 0 4 1
A store() 0 11 2
A show() 0 12 2
A edit() 0 4 1
A update() 0 9 2
A getTags() 0 4 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
/**
13
 * Class TagController
14
 * @package App\Http\Controllers
15
 */
16
class TagController extends ApiController
17
{
18
    /**
19
     * @var TagTransformer
20
     */
21
    protected $tagTransformer;
22
23
    /**
24
     * TagController constructor.
25
     * @param $tagTransformer
26
     */
27
    public function __construct(TagTransformer $tagTransformer)
28
    {
29
        $this->tagTransformer = $tagTransformer;
30
        $this->middleware('auth:api');
31
    }
32
33
    /**
34
     * Display a listing of the resource.
35
     * @param null $taskId
36
     * @return \Illuminate\Http\Response
37
     */
38
    public function index($taskId = null)
39
    {
40
41
        $tag = $this->getTags($taskId);
42
        return $this->respond($this->tagTransformer->transformCollection($tag->all()));
43
    }
44
45
    /**
46
     * Show the form for creating a new resource.
47
     *
48
     * @return \Illuminate\Http\Response
49
     */
50
    public function create()
51
    {
52
        //
53
    }
54
55
    /**
56
     * Store a newly created resource in storage.
57
     * @return \Illuminate\Http\Response
58
     */
59
    public function store()
60
    {
61
        if (!Input::get('title')) {
62
            return $this->setStatusCode(IlluminateResponse::HTTP_UNPROCESSABLE_ENTITY)
63
                ->respondWithError('Parameters failed validation for a task.');
64
        }
65
66
        Tag::create(Input::all());
67
68
        return $this->respondCreated('Task successfully created.');
69
    }
70
71
    /**
72
     * Display the specified resource.
73
     *
74
     * @param  int $id
75
     * @return \Illuminate\Http\Response
76
     */
77
    public function show($id)
78
    {
79
        $tag = Tag::find($id);
80
81
        if (!$tag) {
82
            return $this->respondNotFound('Tag does not exsist');
83
        }
84
85
        return $this->respond([
86
            'data' => $this->tagTransformer->transform($tag)
87
        ]);
88
    }
89
90
    /**
91
     * Show the form for editing the specified resource.
92
     *
93
     * @param  int $id
94
     * @return \Illuminate\Http\Response
95
     */
96
    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...
97
    {
98
        //
99
    }
100
101
    /**
102
     * Update the specified resource in storage.
103
     *
104
     * @param  \Illuminate\Http\Request $request
105
     * @param  int $id
106
     * @return \Illuminate\Http\Response
107
     */
108
    public function update(Request $request, $id)
109
    {
110
        $tag = Tag::find($id);
111
        if (!$tag) {
112
            return $this->respondNotFound('Tag does not exist!');
113
        }
114
        $tag->title = $request->title;
1 ignored issue
show
Bug introduced by
The property title does not seem to exist in Illuminate\Http\Request.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
115
        $tag->save();
116
    }
117
118
    /**
119
     * Remove the specified resource from storage.
120
     *
121
     * @param  int $id
122
     * @return \Illuminate\Http\Response
123
     */
124
    public function destroy($id)
125
    {
126
        Tag::destroy($id);
127
    }
128
129
    /**
130
     * @param $taskId
131
     * @return \Illuminate\Database\Eloquent\Collection|static[]
132
     */
133
    public function getTags($taskId)
134
    {
135
        return $taskId ? Task::findOrFail($taskId)->tags : Tag::all();
136
    }
137
}