ProjectController::myproject()   F
last analyzed

Complexity

Conditions 12
Paths 624

Size

Total Lines 74

Duplication

Lines 4
Ratio 5.41 %

Importance

Changes 0
Metric Value
dl 4
loc 74
rs 3.225
c 0
b 0
f 0
cc 12
nc 624
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Fabrica\Http\Api;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Facades\Schema;
7
use Illuminate\Support\Facades\Event;
8
use Fabrica\Events\IssueEvent;
9
10
use Fabrica\Http\Requests;
11
use Fabrica\Http\Api\Controller;
12
use Fabrica\Project\Eloquent\Project;
13
use Fabrica\Project\Eloquent\UserGroupProject;
14
use Fabrica\Project\Eloquent\AccessProjectLog;
15
use Fabrica\Customization\Eloquent\Type;
16
use Fabrica\Acl\Acl;
17
use Fabrica\Project\Provider;
18
19
use Fabrica\Events\AddUserToRoleEvent;
20
use Fabrica\Events\DelUserFromRoleEvent;
21
use Fabrica\System\Eloquent\SysSetting;
22
use Sentinel;
23
use DB;
24
25
use MongoDB\BSON\ObjectID;
26
use MongoDB\Model\CollectionInfo;
27
28
class ProjectController extends Controller
29
{
30
    public function __construct()
31
    {
32
        $this->middleware('privilege:sys_admin', [ 'only' => [ 'index', 'getOptions', 'updMultiStatus', 'createMultiIndex', 'destroy' ] ]);
33
        parent::__construct();
34
    }
35
36
    /**
37
     * Display a listing of the resource.
38
     *
39
     * @return \Illuminate\Http\Response
40
     */
41
    public function recent(Request $request)
42
    {
43
        // get bound groups
44
        $group_ids = array_column(Acl::getBoundGroups($this->user->id), 'id');
45
        $user_projects = UserGroupProject::whereIn('ug_id', array_merge($group_ids, [ $this->user->id ]))
46
            ->where('link_count', '>', 0)
47
            ->get(['project_key'])
48
            ->toArray();
49
        $pkeys = array_column($user_projects, 'project_key');
50
51
        // get latest access projects
52
        $accessed_projects = AccessProjectLog::where('user_id', $this->user->id)
53
            ->orderBy('latest_access_time', 'desc')
54
            ->get(['project_key'])
55
            ->toArray();
56
        $accessed_pkeys = array_column($accessed_projects, 'project_key');
57
58
        $new_accessed_pkeys = array_unique(array_intersect($accessed_pkeys, $pkeys));
59
60
        $projects = [];
61
        foreach ($new_accessed_pkeys as $pkey)
62
        {
63
            $project = Project::where('key', $pkey)->first();
64
            if (!$project || $project->status === 'closed') {
65
                continue;
66
            }
67
68
            $projects[] = [ 'key' => $project->key, 'name' => $project->name ];
69
            if (count($projects) >= 5) { break; 
70
            }
71
        }
72
73
        return  Response()->json([ 'ecode' => 0, 'data' => $projects ]); 
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...
74
    }
75
76
    /**
77
     * Display a listing of the resource.
78
     *
79
     * @return \Illuminate\Http\Response
80
     */
81
    public function myproject(Request $request)
82
    {
83
        // get bound groups
84
        $group_ids = array_column(Acl::getBoundGroups($this->user->id), 'id');
85
        // fix me
86
        $user_projects = UserGroupProject::whereIn('ug_id', array_merge($group_ids, [ $this->user->id ]))
87
            ->where('link_count', '>', 0)
88
            ->orderBy('created_at', 'asc')
89
            ->get(['project_key'])
90
            ->toArray();
91
92
        $pkeys = array_values(array_unique(array_column($user_projects, 'project_key')));
93
94
        $offset_key = $request->input('offset_key');
95
        if (isset($offset_key)) {
96
            $ind = array_search($offset_key, $pkeys);
97
            if ($ind === false) {
98
                $pkeys = [];
99
            }
100
            else
101
            {
102
                $pkeys = array_slice($pkeys, $ind + 1); 
103
            }
104
        }
105
106
        $limit = $request->input('limit');
107
        if (!isset($limit)) {
108
            $limit = 36;
109
        }
110
        $limit = intval($limit);
111
112
        $status = $request->input('status');
113
        if (!isset($status)) {
114
            $status = 'all';
115
        }
116
117
        $name = $request->input('name');
118
119
        $projects = [];
120
        foreach ($pkeys as $pkey)
121
        {
122
            $query = Project::where('key', $pkey);
123
            if ($name) {
124
                $query->where(
125
                    function ($query) use ($name) {
126
                        $query->where('key', 'like', '%' . $name . '%')->orWhere('name', 'like', '%' . $name . '%');
127
                    }
128
                );
129
            }
130
            if ($status != 'all') {
131
                $query = $query->where('status', $status);
132
            }
133
134
            $project = $query->first();
135
            if (!$project) {
136
                continue;
137
            }
138
139
            $projects[] = $project->toArray();
140
            if (count($projects) >= $limit) {
141
                break;
142
            }
143
        }
144
        
145 View Code Duplication
        foreach ($projects as $key => $project)
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...
146
        {
147
            $projects[$key]['principal']['nameAndEmail'] = $project['principal']['name'] . '(' . $project['principal']['email'] . ')';
148
        }
149
150
        $syssetting = SysSetting::first();
151
        $allow_create_project = isset($syssetting->properties['allow_create_project']) ? $syssetting->properties['allow_create_project'] : 0;
152
153
        return response()->json([ 'ecode' => 0, 'data' => $projects, 'options' => [ 'limit' => $limit, 'allow_create_project' => $allow_create_project ] ]);
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...
154
    }
155
156
    /**
157
     * get the options of project.
158
     *
159
     * @return \Illuminate\Http\Response
160
     */
161
    public function getOptions(Request $request)
162
    {
163
        $principals = Project::distinct('principal')->get([ 'principal' ])->toArray();
164
165
        $newPrincipals = [];
166
        foreach ($principals as $principal)
167
        {
168
            $tmp = [];
169
            $tmp['id'] = $principal['id'];
170
            $tmp['name'] = $principal['name'];
171
            $tmp['email'] = $principal['email'];
172
            $newPrincipals[] = $tmp;
173
        }
174
175
        return response()->json([ 'ecode' => 0, 'data' => [ 'principals' => $newPrincipals ] ]);
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...
176
    }
177
178
    /**
179
     * create index of the project.
180
     *
181
     * @return \Illuminate\Http\Response
182
     */
183
    public function createIndex(Request $request, $id)
184
    {
185
        $project = Project::find($id);
186
        if (!$project) {
187
            throw new \UnexpectedValueException('the project does not exist.', -14006);
188
        }
189 View Code Duplication
        if ($project->principal['id'] !== $this->user->id && !$this->user->hasAccess('sys_admin')) {
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...
190
            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...
191
        }
192
193
        Schema::collection(
194 View Code Duplication
            'issue_' . $project->key, function ($col) {
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...
195
                $col->index('type');
196
                $col->index('state');
197
                $col->index('resolution');
198
                $col->index('priority');
199
                $col->index('created_at');
200
                $col->index('updated_at');
201
                $col->index('epic');
202
                $col->index('module');
203
                $col->index('resolve_version');
204
                $col->index('labels');
205
                $col->index('no');
206
                $col->index('parent_id');
207
                $col->index('assignee.id');
208
                $col->index('reporter.id');
209
            }
210
        );
211
        Schema::collection(
212
            'activity_' . $project->key, function ($col) {
213
                $col->index('event_key');
214
            }
215
        );
216
        Schema::collection(
217
            'comments_' . $project->key, function ($col) {
218
                $col->index('issue_id');
219
            }
220
        );
221
        Schema::collection(
222
            'issue_his_' . $project->key, function ($col) {
223
                $col->index('issue_id');
224
            }
225
        );
226
        Schema::collection(
227
            'document_' . $project->key, function ($col) {
228
                $col->index('parent');
229
            }
230
        );
231
        Schema::collection(
232
            'wiki_' . $project->key, function ($col) {
233
                $col->index('parent');
234
            }
235
        );
236
237
        return response()->json([ 'ecode' => 0, 'data' => $project ]);
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...
238
    }
239
240
    /**
241
     * create index of all selected projects.
242
     *
243
     * @return \Illuminate\Http\Response
244
     */
245
    public function createMultiIndex(Request $request)
246
    {
247
        $ids = $request->input('ids');
248
        if (!isset($ids) || !$ids) {
249
            throw new \InvalidArgumentException('the selected projects cannot been empty.', -14007);
250
        }
251
252
        foreach ($ids as $id)
253
        {
254
            $project = Project::find($id);
255
            if (!$project) {
256
                continue;
257
            }
258
259
            Schema::collection(
260 View Code Duplication
                'issue_' . $project->key, function ($col) {
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...
261
                    $col->index('type');
262
                    $col->index('state');
263
                    $col->index('resolution');
264
                    $col->index('priority');
265
                    $col->index('created_at');
266
                    $col->index('updated_at');
267
                    $col->index('module');
268
                    $col->index('epic');
269
                    $col->index('resolve_version');
270
                    $col->index('labels');
271
                    $col->index('no');
272
                    $col->index('assignee.id');
273
                    $col->index('reporter.id');
274
                }
275
            );
276
            Schema::collection(
277
                'activity_' . $project->key, function ($col) {
278
                    $col->index('event_key');
279
                }
280
            );
281
            Schema::collection(
282
                'comments_' . $project->key, function ($col) {
283
                    $col->index('issue_id');
284
                }
285
            );
286
            Schema::collection(
287
                'issue_his_' . $project->key, function ($col) {
288
                    $col->index('issue_id');
289
                }
290
            );
291
            Schema::collection(
292
                'document_' . $project->key, function ($col) {
293
                    $col->index('parent');
294
                }
295
            );
296
            Schema::collection(
297
                'wiki_' . $project->key, function ($col) {
298
                    $col->index('parent');
299
                }
300
            );
301
        }
302
        return response()->json([ 'ecode' => 0, 'data' => [ 'ids' => $ids ] ]);
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...
303
    }
304
305
    /**
306
     * update status of all selected projects.
307
     *
308
     * @return \Illuminate\Http\Response
309
     */
310
    public function updMultiStatus(Request $request)
311
    {
312
        $ids = $request->input('ids');
313
        if (!isset($ids) || !$ids) {
314
            throw new \InvalidArgumentException('the selected projects cannot been empty.', -14007);
315
        }
316
317
        $status = $request->input('status');
318
        if (!isset($status) || !$status) {
319
            throw new \InvalidArgumentException('the status cannot be empty.', -14008);
320
        }
321
322
        $newIds = [];
323
        foreach ($ids as $id)
324
        {
325
            $newIds[] = new ObjectID($id);
326
        }
327
328
        Project::whereRaw([ '_id' => [ '$in' => $newIds ] ])->update([ 'status' => $status ]);
329
330
        return response()->json([ 'ecode' => 0, 'data' => [ 'ids' => $ids ] ]);
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...
331
    }
332
333
    /**
334
     * search project by the name or code.
335
     *
336
     * @return \Illuminate\Http\Response
337
     */
338
    public function search(Request $request)
339
    {
340
        $query = DB::collection('project');
341
342
        $s = $request->input('s');
343 View Code Duplication
        if (isset($s) && $s) {
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...
344
            $query->where(
345
                function ($query) use ($s) {
346
                    $query->where('key', 'like', '%' . $s . '%')->orWhere('name', 'like', '%' . $s . '%');
347
                }
348
            );
349
        }
350
351
        $projects = $query->take(10)->get([ 'name', 'key' ]);
352
353
        return response()->json([ 'ecode' => 0, 'data' => parent::arrange($projects) ]);
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 search()). 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...
354
    }
355
356
    /**
357
     * Display a listing of the resource.
358
     *
359
     * @return \Illuminate\Http\Response
360
     */
361
    public function index(Request $request)
362
    {
363
        $query = DB::collection('project');
364
365
        $principal_id = $request->input('principal_id');
366
        if (isset($principal_id) && $principal_id) {
367
            $query = $query->where('principal.id', $principal_id);
368
        }
369
370
        $status = $request->input('status');
371
        if (isset($status) && $status !== 'all') {
372
            $query = $query->where('status', $status);
373
        }
374
375
        $name = $request->input('name');
376 View Code Duplication
        if (isset($name) && $name) {
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...
377
            $query->where(
378
                function ($query) use ($name) {
379
                    $query->where('key', 'like', '%' . $name . '%')->orWhere('name', 'like', '%' . $name . '%');
380
                }
381
            );
382
        }
383
384
        // get total
385
        $total = $query->count();
386
387
        $query->orderBy('created_at', 'desc');
388
389
        $page_size = 30;
390
        $page = $request->input('page') ?: 1;
391
        $query = $query->skip($page_size * ($page - 1))->take($page_size);
392
        $projects = $query->get([ 'name', 'key', 'description', 'status', 'principal' ]);
393 View Code Duplication
        foreach ($projects as $key => $project)
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...
394
        {
395
            $projects[$key]['principal']['nameAndEmail'] = $project['principal']['name'] . '(' . $project['principal']['email'] . ')';
396
        }
397
398
        return response()->json([ 'ecode' => 0, 'data' => parent::arrange($projects), 'options' => [ 'total' => $total, 'sizePerPage' => $page_size ] ]);
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...
399
    }
400
401
    /**
402
     * Store a newly created resource in storage.
403
     *
404
     * @param  \Illuminate\Http\Request $request
405
     * @return \Illuminate\Http\Response
406
     */
407
    public function store(Request $request)
408
    {
409
        $syssetting = SysSetting::first();
410
        $allow_create_project = isset($syssetting->properties['allow_create_project']) ? $syssetting->properties['allow_create_project'] : 0;        
411
        if ($allow_create_project !== 1 && !$this->user->hasAccess('sys_admin')) {
412
            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...
413
        }
414
415
        $insValues = [];
416
417
        $name = $request->input('name');
418
        if (!$name) {
419
            throw new \UnexpectedValueException('the name can not be empty.', -14000);
420
        }
421
        $insValues['name'] = $name;
422
423
        $key = $request->input('key');
424
        if (!$key) {
425
            throw new \InvalidArgumentException('project key cannot be empty.', -14001);
426
        }
427
        if (Project::Where('key', $key)->exists()) {
428
            throw new \InvalidArgumentException('project key has been taken.', -14002);
429
        }
430
        $insValues['key'] = $key;
431
432
        $principal = $request->input('principal');
433
        if (!isset($principal) || !$principal) {
434
            $insValues['principal'] = [ 'id' => $this->user->id, 'name' => $this->user->first_name, 'email' => $this->user->email ];
435
        }
436
        else
437
        {
438
            $principal_info = Sentinel::findById($principal);
439
            if (!$principal_info) {
440
                throw new \InvalidArgumentException('the user is not exists.', -14003);
441
            }
442
            $insValues['principal'] = [ 'id' => $principal_info->id, 'name' => $principal_info->first_name, 'email' => $principal_info->email ];
443
        }
444
445
        $description = $request->input('description');
446
        if (isset($description) && $description) {
447
            $insValues['description'] = $description;
448
        }
449
450
        $insValues['category'] = 1;
451
        $insValues['status'] = 'active';
452
        $insValues['creator'] = [ 'id' => $this->user->id, 'name' => $this->user->first_name, 'email' => $this->user->email ];
453
454
        // save the project
455
        $project = Project::create($insValues); //fix me
456
        // add issue-type template to project
457
        $this->initialize($project->key);
458
        // trigger add user to usrproject
459
        Event::fire(new AddUserToRoleEvent([ $insValues['principal']['id'] ], $key));
460
461
        if (isset($project->principal)) {
462
            $project->principal = array_merge($insValues['principal'], [ 'nameAndEmail' => $insValues['principal']['name'] . '(' . $insValues['principal']['email'] . ')' ]);
463
        }
464
465
        return response()->json([ 'ecode' => 0, 'data' => $project ]);
466
    }
467
468
    /**
469
     * initialize project data.
470
     *
471
     * @param  string $key
472
     * @param  int    $id
0 ignored issues
show
Bug introduced by
There is no parameter named $id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
473
     * @return 
474
     */
475
    public function initialize($key)
476
    {
477
        $types = Type::where('project_key', '$_sys_$')->get()->toArray();
478
        foreach ($types as $type)
479
        {
480
            Type::create(array_only($type, [ 'name', 'abb', 'screen_id', 'workflow_id', 'sn', 'type', 'disabled', 'default' ]) + [ 'project_key' => $key ]);
481
        }
482
    }
483
484
    /**
485
     * Display the specified resource.
486
     *
487
     * @param  int $id
0 ignored issues
show
Bug introduced by
There is no parameter named $id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
488
     * @return \Illuminate\Http\Response
489
     */
490
    public function show($key)
491
    {
492
        $project = Project::where('key', $key)->first();
493
        if (!$project) {
494
            return response()->json(['ecode' => -14004, 'emsg' => 'the project does not exist.']);
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...
495
        }
496
497
        if ($project->status !== 'active') {
498
            return response()->json(['ecode' => -14009, 'emsg' => 'the project has been closed.']);
499
        }
500
501
        // get action allow of the project.
502
        $permissions = Acl::getPermissions($this->user->id, $project->key);
503
        if ($this->user->id === $project->principal['id'] || $this->user->email === '[email protected]') {
504
            !in_array('view_project', $permissions) && $permissions[] = 'view_project';
505
            !in_array('manage_project', $permissions) && $permissions[] = 'manage_project';
506
        }
507
508
        //if (!$permissions)
509
        //{
510
        //    $isMember = UserProject::where('user_id', $this->user->id)
511
        //        ->where('project_key', $key)
512
        //        ->where('link_count', '>', 0)
513
        //        ->exists();
514
        //    if ($isMember)
515
        //    {
516
        //        $permissions[] = 'view_project';
517
        //    }
518
        //}
519
        // get searchers
520
        //$searchers = DB::collection('searcher_' . $key)->where('user', $this->user->id)->orderBy('created_at', 'asc')->get();
521
        // get project users
522
        //$users = Provider::getUserList($project->key);
523
        // get state list
524
        //$states = Provider::getStateList($project->key, ['name']);
525
        // get resolution list
526
        //$resolutions = Provider::getResolutionList($project->key, ['name']);
527
        // get priority list
528
        //$priorities = Provider::getPriorityList($project->key, ['color', 'name']);
529
        // get version list
530
        //$versions = Provider::getVersionList($project->key, ['name']);
531
        // get module list
532
        //$modules = Provider::getModuleList($project->key, ['name']);
533
        // get project types
534
        //$types = Provider::getTypeListExt($project->key, [ 'assignee' => $users, 'state' => $states, 'resolution' => $resolutions, 'priority' => $priorities, 'version' => $versions, 'module' => $modules ]);
535
536
        // record the project access date
537
        if (in_array('view_project', $permissions)) {
538
            AccessProjectLog::where('project_key', $key)
539
                ->where('user_id', $this->user->id)
540
                ->delete();
541
            AccessProjectLog::create([ 'project_key' => $key, 'user_id' => $this->user->id, 'latest_access_time' => time() ]);
542
        }
543
544
        return response()->json([ 'ecode' => 0, 'data' => $project, 'options' => parent::arrange([ 'permissions' => $permissions ]) ]);
0 ignored issues
show
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...
545
    }
546
547
    /**
548
     * Update the specified resource in storage.
549
     *
550
     * @param  \Illuminate\Http\Request $request
551
     * @param  int                      $id
552
     * @return \Illuminate\Http\Response
553
     */
554
    public function update(Request $request, $id)
555
    {
556
        $updValues = [];
557
        $name = $request->input('name');
558 View Code Duplication
        if (isset($name)) {
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...
559
            if (!$name) {
560
                throw new \UnexpectedValueException('the name can not be empty.', -14000);
561
            }
562
            $updValues['name'] = $name;
563
        }
564
        // check is user is available
565
        $principal = $request->input('principal');
566
        if (isset($principal)) {
567
            if (!$principal) {
568
                throw new \InvalidArgumentException('the principal must be appointed.', -14005);
569
            }
570
571
            $principal_info = Sentinel::findById($principal);
572
            if (!$principal_info) {
573
                throw new \InvalidArgumentException('the user is not exists.', -14003);
574
            }
575
            $updValues['principal'] = [ 'id' => $principal_info->id, 'name' => $principal_info->first_name, 'email' =>  $principal_info->email ]; 
576
        }
577
578
        $description = $request->input('description');
579
        if (isset($description)) {
580
            $updValues['description'] = $description;
581
        }
582
583
        $status = $request->input('status');
584
        if (isset($status) && in_array($status, [ 'active', 'closed' ])) {
585
            $updValues['status'] = $status;
586
        }
587
588
        $project = Project::find($id);
589
        if (!$project) {
590
            throw new \UnexpectedValueException('the project does not exist.', -14004);
591
        }
592 View Code Duplication
        if ($project->principal['id'] !== $this->user->id && !$this->user->hasAccess('sys_admin')) {
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...
593
            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...
594
        }
595
596
        $old_principal = $project->principal;
597
        $project->fill($updValues)->save();
598
599
        if (isset($principal)) {
600
            if ($old_principal['id'] != $principal) {
601
                Event::fire(new AddUserToRoleEvent([ $principal ], $project->key));
602
                Event::fire(new DelUserFromRoleEvent([ $old_principal['id'] ], $project->key));
603
            }
604
        }
605
606
        return response()->json([ 'ecode' => 0, 'data' => Project::find($id) ]);
607
    }
608
609
    /**
610
     * Remove the specified resource from storage.
611
     *
612
     * @param  int $id
613
     * @return \Illuminate\Http\Response
614
     */
615
    public function destroy($id)
616
    {
617
        $project = Project::find($id);
618
        if (!$project) {
619
            throw new \UnexpectedValueException('the project does not exist.', -14004);
620
        }
621
622
        $project_key = $project->key;
623
        //$related_cols = [ 'version', 'module', 'board', 'epic', 'sprint', 'sprint_log', 'searcher', 'access_project_log', 'access_board_log', 'user_group_project', 'watch', 'acl_role', 'acl_roleactor', 'acl_role_permissions', 'oswf_definition' ];
624
        $unrelated_cols = [ 'system.indexes', 'users', 'persistences', 'throttle', 'project' ];
625
        // delete releted table
626
        $collections = DB::listCollections();
627
        foreach ($collections as $col)
628
        {
629
            $col_name = $col->getName();
630
            if (strpos($col_name, 'issue_') === 0 
631
                || strpos($col_name, 'activity_') === 0 
632
                || strpos($col_name, 'comments_') === 0 
633
                || strpos($col_name, 'document_') === 0 
634
                || strpos($col_name, 'wiki_') === 0 
635
                || in_array($col_name, $unrelated_cols)
636
            ) {
637
                continue;
638
            }
639
    
640
            DB::collection($col_name)->where('project_key', $project_key)->delete();
641
        }
642
643
        // delete the collections
644
        Schema::dropIfExists('issue_' . $project_key);
645
        Schema::dropIfExists('issue_his_' . $project_key);
646
        Schema::dropIfExists('activity_' . $project_key);
647
        Schema::dropIfExists('comments_' . $project_key);
648
        Schema::dropIfExists('document_' . $project_key);
649
        Schema::dropIfExists('wiki_' . $project_key);
650
        // delete from the project table
651
        Project::destroy($id);
652
653
        return response()->json([ 'ecode' => 0, 'data' => [ 'id' => $id ] ]);
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...
654
    }
655
656
    /**
657
     * check if project key has been taken 
658
     *
659
     * @param  string $key
660
     * @return \Illuminate\Http\Response
661
     */
662
    public function checkKey($key)
663
    {
664
        $isExisted = Project::Where('key', $key)->exists(); 
665
        return response()->json([ 'ecode' => 0, 'data' => [ 'flag' => $isExisted ? '2' : '1' ] ]);
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...
666
    }
667
}
668