CommentsController::show()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Fabrica\Http\Api;
4
5
use DB;
6
use Fabrica\Events\IssueEvent;
7
use Fabrica\Http\Api\Controller;
8
9
use Fabrica\Http\Requests;
10
use Fabrica\Project\Provider;
11
12
use Illuminate\Http\Request;
13
use Illuminate\Support\Facades\Event;
14
use Sentinel;
15
16
class CommentsController extends Controller
17
{
18
    /**
19
     * Display a listing of the resource.
20
     *
21
     * @return \Illuminate\Http\Response
22
     */
23 View Code Duplication
    public function index(Request $request, $project_key, $issue_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...
24
    {
25
        $sort = ($request->input('sort') === 'asc') ? 'asc' : 'desc';
26
27
        $comments = DB::collection('comments_' . $project_key)
28
            ->where('issue_id', $issue_id)
29
            ->orderBy('created_at', $sort)
30
            ->get();
31
32
        return response()->json([ 'ecode' => 0, 'data' => parent::arrange($comments), 'options' => [ 'current_time' => time() ] ]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
Comprehensibility Bug introduced by
It seems like you call parent on a different method (arrange() instead of index()). Are you sure this is correct? If so, you might want to change this to $this->arrange().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
33
    }
34
35
    /**
36
     * Store a newly created resource in storage.
37
     *
38
     * @param  \Illuminate\Http\Request $request
39
     * @return \Illuminate\Http\Response
40
     */
41
    public function store(Request $request, $project_key, $issue_id)
42
    {
43
        if (!$this->isPermissionAllowed($project_key, 'add_comments')) {
44
            return response()->json(['ecode' => -10002, 'emsg' => 'permission denied.']);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
45
        }
46
47
        $contents = $request->input('contents');
48
        if (!$contents) {
49
            throw new \UnexpectedValueException('the contents can not be empty.', -11200);
50
        }
51
52
        $creator = [ 'id' => $this->user->id, 'name' => $this->user->first_name, 'email' => $this->user->email ];
53
54
        $table = 'comments_' . $project_key;
55
56
        $id = DB::collection($table)->insertGetId(array_only($request->all(), [ 'contents', 'atWho' ]) + [ 'issue_id' => $issue_id, 'creator' => $creator, 'created_at' => time() ]);
57
58
        // trigger event of comments added
59
        Event::fire(new IssueEvent($project_key, $issue_id, $creator, [ 'event_key' => 'add_comments', 'data' => array_only($request->all(), [ 'contents', 'atWho' ]) ])); 
60
61
        $comments = DB::collection($table)->find($id);
62
        return response()->json([ 'ecode' => 0, 'data' => parent::arrange($comments) ]);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (arrange() instead of store()). Are you sure this is correct? If so, you might want to change this to $this->arrange().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
63
    }
64
65
    /**
66
     * Display the specified resource.
67
     *
68
     * @param  int $id
69
     * @return \Illuminate\Http\Response
70
     */
71
    public function show($project_key, $id)
72
    {
73
        $comments = DB::collection('comments_' . $project_key)->find($id);
74
        return response()->json(['ecode' => 0, 'data' => parent::arrange($comments)]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
Comprehensibility Bug introduced by
It seems like you call parent on a different method (arrange() instead of show()). Are you sure this is correct? If so, you might want to change this to $this->arrange().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
75
    }
76
77
    /**
78
     * Update the specified resource in storage.
79
     *
80
     * @param  \Illuminate\Http\Request $request
81
     * @param  int                      $id
82
     * @return \Illuminate\Http\Response
83
     */
84
    public function update(Request $request, $project_key, $issue_id, $id)
85
    {
86
        $comments = DB::collection('comments_' . $project_key)->find($id);
87
        if (!$comments) {
88
            throw new \UnexpectedValueException('the comments does not exist or is not in the project.', -11201);
89
        }
90
91
        $contents = $request->input('contents');
92
        if (isset($contents)) {
93
            if (!$contents) {
94
                throw new \UnexpectedValueException('the contents can not be empty.', -11200);
95
            }
96
        }
97
        // record the changed comments
98
        $changedComments = [];
99
100
        $table = 'comments_' . $project_key;
101
        $user = [ 'id' => $this->user->id, 'name' => $this->user->first_name, 'email' => $this->user->email ];
102
        $operation = $request->input('operation');
103
        if (isset($operation)) {
104
            if (!in_array($operation, [ 'addReply', 'editReply', 'delReply' ])) {
105
                throw new \UnexpectedValueException('the operation is incorrect value.', -11204);
106
            }
107
            if (!isset($comments['reply']) || !$comments['reply']) {
108
                $comments['reply'] = [];
109
            }
110
111
            if ($operation == 'addReply') {
112
                if (!$this->isPermissionAllowed($project_key, 'add_comments')) {
113
                    return response()->json(['ecode' => -10002, 'emsg' => 'permission denied.']);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
114
                }
115
116
                $reply_id = md5(microtime() . $this->user->id); 
117
                array_push($comments['reply'], array_only($request->all(), [ 'contents', 'atWho' ]) + [ 'id' => $reply_id , 'creator' => $user, 'created_at' => time() ]);
118
                $changedComments = array_only($request->all(), [ 'contents', 'atWho' ]) + [ 'to' => $comments['creator'] ];
119
            } 
120
            else if ($operation == 'editReply') {
121
                $reply_id = $request->input('reply_id');
122
                if (!isset($reply_id) || !$reply_id) {
123
                    throw new \UnexpectedValueException('the reply id can not be empty.', -11202);
124
                }
125
                $index = $this->array_find([ 'id' => $reply_id ], $comments['reply']); 
126
                if ($index !== false) {
127
                    if (!$this->isPermissionAllowed($project_key, 'edit_comments') && !($comments['reply'][$index]['creator']['id'] == $this->user->id && $this->isPermissionAllowed($project_key, 'edit_self_comments'))) {
128
                        return response()->json(['ecode' => -10002, 'emsg' => 'permission denied.']);
129
                    }
130
               
131
                    $comments['reply'][$index] = array_merge($comments['reply'][$index], [ 'updated_at' => time(), 'edited_flag' => 1 ] + array_only($request->all(), [ 'contents', 'atWho' ]));
132
                    $changedComments = array_only($comments['reply'][$index], [ 'contents', 'atWho' ]) + [ 'to' => $comments['creator'] ];
133
                }
134
                else
135
                {
136
                    throw new \UnexpectedValueException('the reply does not exist', -11203);
137
                }
138
            }
139
            else if ($operation == 'delReply') {
140
                $reply_id = $request->input('reply_id');
141
                if (!isset($reply_id) || !$reply_id) {
142
                    throw new \UnexpectedValueException('the reply id can not be empty.', -11202);
143
                }
144
                $index = $this->array_find([ 'id' => $reply_id ], $comments['reply']); 
145
                if ($index !== false) {
146
                    if (!$this->isPermissionAllowed($project_key, 'delete_comments') && !($comments['reply'][$index]['creator']['id'] == $this->user->id && $this->isPermissionAllowed($project_key, 'delete_self_comments'))) {
147
                        return response()->json(['ecode' => -10002, 'emsg' => 'permission denied.']);
148
                    }
149
150
                    $changedComments = array_only($comments['reply'][$index], [ 'contents', 'atWho' ]) + [ 'to' => $comments['creator'] ];
151
                    array_splice($comments['reply'], $index, 1);
152
                }
153
                else
154
                {
155
                    throw new \UnexpectedValueException('the reply does not exist', -11203);
156
                }
157
            }
158
            DB::collection($table)->where('_id', $id)->update([ 'reply' => $comments['reply'] ]);
159
        }
160
        else
161
        {
162 View Code Duplication
            if (!$this->isPermissionAllowed($project_key, 'edit_comments') && !($comments['creator']['id'] == $this->user->id && $this->isPermissionAllowed($project_key, 'edit_self_comments'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
163
                return response()->json(['ecode' => -10002, 'emsg' => 'permission denied.']);
164
            }
165
166
            DB::collection($table)->where('_id', $id)->update([ 'updated_at' => time(), 'edited_flag' => 1 ] + array_only($request->all(), [ 'contents', 'atWho' ]));
167
            $changedComments = array_only($request->all(), [ 'contents', 'atWho' ]);
168
        }
169
170
        // trigger event of comments 
171
        $event_key = '';
172
        if (isset($operation)) {
173
            $operation === 'addReply'   && $event_key = 'add_comments';
174
            $operation === 'editReply'  && $event_key = 'edit_comments';
175
            $operation === 'delReply'   && $event_key = 'del_comments';
176
        }
177
        else
178
        {
179
            $event_key = 'edit_comments';
180
        }
181
        Event::fire(new IssueEvent($project_key, $issue_id, $user, [ 'event_key' => $event_key, 'data' => $changedComments ]));
182
183
        return response()->json([ 'ecode' => 0, 'data' => parent::arrange(DB::collection($table)->find($id)) ]);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (arrange() instead of update()). Are you sure this is correct? If so, you might want to change this to $this->arrange().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
184
    }
185
186
    /**
187
     * Remove the specified resource from storage.
188
     *
189
     * @param  int $id
190
     * @return \Illuminate\Http\Response
191
     */
192
    public function destroy($project_key, $issue_id, $id)
193
    {
194
        $table = 'comments_' . $project_key;
195
        $comments = DB::collection($table)->find($id);
196
        if (!$comments) {
197
            throw new \UnexpectedValueException('the comments does not exist or is not in the project.', -11201);
198
        }
199
200 View Code Duplication
        if (!$this->isPermissionAllowed($project_key, 'manage_project') && !($comments['creator']['id'] == $this->user->id && $this->isPermissionAllowed($project_key, 'delete_self_comments'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
201
            return response()->json(['ecode' => -10002, 'emsg' => 'permission denied.']);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
202
        }
203
204
        DB::collection($table)->where('_id', $id)->delete();
205
206
        $user = [ 'id' => $this->user->id, 'name' => $this->user->first_name, 'email' => $this->user->email ];
207
        // trigger the event of del comments
208
        Event::fire(new IssueEvent($project_key, $issue_id, $user, [ 'event_key' => 'del_comments', 'data' => array_only($comments, [ 'contents', 'atWho' ]) ])); 
209
210
        return response()->json(['ecode' => 0, 'data' => ['id' => $id]]);
211
    }
212
213
    /**
214
     * define the array function of searching for array object.
215
     *
216
     * @param  array $needle
217
     * @param  array $haystack
218
     * @return \Illuminate\Http\Response
219
     */
220
    public function array_find($needle, $haystack)
221
    {
222
        foreach($haystack as $key => $val)
223
        {
224
            if ($needle['id'] == $val['id']) {
225
                return $key;
226
            }
227
        }
228
        return false;
229
    }
230
}
231