DocumentController::downloadThumbnails()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 3
nc 3
nop 3
1
<?php
2
3
namespace Fabrica\Http\Api;
4
5
use Illuminate\Http\Request;
6
7
use Fabrica\Http\Requests;
8
use Fabrica\Http\Api\Controller;
9
use Fabrica\Events\DocumentEvent;
10
use Fabrica\Project\Eloquent\DocumentFavorites;
11
use Fabrica\Acl\Acl;
12
use DB;
13
use Fabrica\Utils\File;
14
15
use MongoDB\BSON\ObjectID;
16
use Zipper;
17
18
class DocumentController extends Controller
19
{
20
    /**
21
     * search path.
22
     *
23
     * @param  \Illuminate\Http\Request $request
24
     * @param  string                   $project_key
25
     * @return \Illuminate\Http\Response
26
     */
27 View Code Duplication
    public function searchPath(Request $request, $project_key)
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...
28
    {
29
        $s =  $request->input('s');
30
        if (!$s) {
31
            return response()->json(['ecode' => 0, 'data' => []]);
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...
32
        }
33
34
        if ($s === '/') {
35
            return response()->json(['ecode' => 0, 'data' => [ [ 'id' => '0', 'name' => '/' ] ] ]);
36
        }
37
38
        $query = DB::collection('document_' . $project_key)
39
            ->where('d', 1)
40
            ->where('del_flag', '<>', 1)
41
            ->where('name', 'like', '%' . $s . '%');
42
43
        $moved_path = $request->input('moved_path');
44
        if (isset($moved_path) && $moved_path) {
45
            $query->where('pt', '<>', $moved_path);
46
            $query->where('_id', '<>', $moved_path);
47
        }
48
49
        $directories = $query->take(20)->get(['name', 'pt']);
50
51
        $ret = [];
52
        foreach ($directories as $d)
53
        {
54
            $parents = [];
55
            $path = '';
56
            $ps = DB::collection('document_' . $project_key)
57
                ->whereIn('_id', $d['pt'])
58
                ->get([ 'name' ]);
59
            foreach ($ps as $val)
60
            {
61
                $parents[$val['_id']->__toString()] = $val['name'];
62
            }
63
64
            foreach ($d['pt'] as $pid)
65
            {
66
                if (isset($parents[$pid])) {
67
                    $path .= '/' . $parents[$pid];
68
                }
69
            }
70
            $path .= '/' . $d['name'];
71
            $ret[] = [ 'id' => $d['_id']->__toString(), 'name' => $path ];
72
        }
73
        return response()->json(['ecode' => 0, 'data' => parent::arrange($ret)]);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (arrange() instead of searchPath()). 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...
74
    }
75
76
    /**
77
     * get the directory children.
78
     *
79
     * @param  string $project_key
80
     * @param  string $directory
81
     * @return \Illuminate\Http\Response
82
     */
83
    public function getDirChildren(Request $request, $project_key, $directory)
84
    {
85
        $sub_dirs = DB::collection('document_' . $project_key)
86
            ->where('parent', $directory)
87
            ->where('d', 1)
88
            ->where('del_flag', '<>', 1)
89
            ->get();
90
91
        $res = [];
92
        foreach ($sub_dirs as $val) 
93
        {
94
            $res[] = [ 'id' => $val['_id']->__toString(), 'name' => $val['name'] ];
95
        }
96
97
        return response()->json([ 'ecode' => 0, 'data' => $res ]);
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...
98
    }
99
100
    /**
101
     * get the directory tree.
102
     *
103
     * @param  string $project_key
104
     * @return \Illuminate\Http\Response
105
     */
106
    public function getDirTree(Request $request, $project_key)
107
    {
108
        $dt = [ 'id' => '0', 'name' => '根目录' ];
109
110
        $curdir = $request->input('currentdir');
111
        if (!$curdir) {
112
            $curdir = '0';
113
        }
114
115
        $pt = [ '0' ];
116
        if ($curdir !== '0') {
117
            $node = DB::collection('document_' . $project_key)
118
                ->where('_id', $curdir)
119
                ->first();
120
121
            if ($node) {
122
                $pt = $node['pt'];
123
                array_push($pt, $curdir);
124
            }
125
        }
126
127 View Code Duplication
        foreach($pt as $val)
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...
128
        {
129
            $sub_dirs = DB::collection('document_' . $project_key)
130
                ->where('parent', $val)
131
                ->where('d', 1)
132
                ->where('del_flag', '<>', 1)
133
                ->get();
134
135
            $this->addChildren2Tree($dt, $val, $sub_dirs);
136
        }
137
138
        return response()->json([ 'ecode' => 0, 'data' => $dt ]);
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...
139
    }
140
141
    /**
142
     * add children to tree.
143
     *
144
     * @param  array  $dt
145
     * @param  string $parent_id
146
     * @param  array  $sub_dirs
147
     * @return void
148
     */
149
    public function addChildren2Tree(&$dt, $parent_id, $sub_dirs)
150
    {
151
        $new_dirs = [];
152
        foreach($sub_dirs as $val)
153
        {
154
            $new_dirs[] = [ 'id' => $val['_id']->__toString(), 'name' => $val['name'] ];
155
        }
156
157 View Code Duplication
        if ($dt['id'] == $parent_id) {
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...
158
            $dt['children'] = $new_dirs;
159
            return true;
160
        }
161
        else
162
        {
163
            if (isset($dt['children']) && $dt['children']) {
164
                $children_num = count($dt['children']);
165
                for ($i = 0; $i < $children_num; $i++)
166
                {
167
                    $res = $this->addChildren2Tree($dt['children'][$i], $parent_id, $sub_dirs);
168
                    if ($res === true) {
169
                        return true;
170
                    }
171
                }
172
            }
173
            return false;
174
        }
175
    }
176
177
    /**
178
     * Display a listing of the resource.
179
     *
180
     * @return \Illuminate\Http\Response
181
     */
182
    public function getOptions(Request $request, $project_key)
183
    {
184
        $uploaders = DB::collection('document_' . $project_key)
185
            ->where('del_flag', '<>', 1)
186
            ->distinct('uploader')
187
            ->get([ 'uploader' ]);
188
189
        return response()->json(['ecode' => 0, 'data' => [ 'uploader' => $uploaders ]]);
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...
190
    }
191
192
    /**
193
     * Display a listing of the resource.
194
     *
195
     * @return \Illuminate\Http\Response
196
     */
197
    public function index(Request $request, $project_key, $directory)
198
    {
199
        $documents = [];
0 ignored issues
show
Unused Code introduced by
$documents is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
200
        $mode = 'list';
201
        $query = DB::collection('document_' . $project_key);
202
203
        $uploader_id = $request->input('uploader_id');
204
        if (isset($uploader_id) && $uploader_id) {
205
            $mode = 'search';
206
            $query->where('uploader.id', $uploader_id == 'me' ? $this->user->id : $uploader_id);
207
        }
208
209
        $name = $request->input('name');
210 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...
211
            $mode = 'search';
212
            $query = $query->where('name', 'like', '%' . $name . '%');
213
        }
214
215
        $uploaded_at = $request->input('uploaded_at');
216
        if (isset($uploaded_at) && $uploaded_at) {
217
            $mode = 'search';
218
            $unitMap = [ 'w' => 'week', 'm' => 'month', 'y' => 'year' ];
219
            $unit = substr($uploaded_at, -1);
220
            $val = abs(substr($uploaded_at, 0, -1));
221
            $query->where('uploaded_at', '>=', strtotime(date('Ymd', strtotime('-' . $val . ' ' . $unitMap[$unit]))));
222
        }
223
224
        $favorite_documents = DocumentFavorites::where('project_key', $project_key)
225
            ->where('user.id', $this->user->id)
226
            ->get()
227
            ->toArray();
228
        $favorite_dids = array_column($favorite_documents, 'did');
229
230
        $myfavorite = $request->input('myfavorite');
231 View Code Duplication
        if (isset($myfavorite) && $myfavorite == '1') {
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...
232
            $mode = 'search';
233
            $favoritedIds = [];
234
            foreach ($favorite_dids as $did)
235
            {
236
                $favoritedIds[] = new ObjectID($did);
237
            }
238
239
            $query->whereIn('_id', $favoritedIds);
240
        }
241
242 View Code Duplication
        if ($directory !== '0') {
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...
243
            $query = $query->where($mode === 'search' ? 'pt' : 'parent', $directory);
244
        }
245
        else
246
        {
247
            if ($mode === 'list') {
248
                $query = $query->where('parent', $directory);
249
            }
250
        }
251
252
        $query->where('del_flag', '<>', 1);
253
        $query->orderBy('d', 'desc')->orderBy('_id', 'desc');
254
255
        $limit = 1000; // fix me
256
        $query->take($limit);
257
        $documents = $query->get();
258
259 View Code Duplication
        foreach ($documents as $k => $d)
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...
260
        {
261
            if (in_array($d['_id']->__toString(), $favorite_dids)) {
262
                $documents[$k]['favorited'] = true;
263
            }
264
        }
265
266
        $path = [];
267
        if ($directory === '0') {
268
            $path[] = [ 'id' => '0', 'name' => 'root' ];
269
        }
270
        else
271
        {
272
            $path[] = [ 'id' => '0', 'name' => 'root' ];
273
            $d = DB::collection('document_' . $project_key)
274
                ->where('_id', $directory)
275
                ->first();
276
            if ($d && isset($d['pt'])) {
277
                $parents = [];
278
                $ps = DB::collection('document_' . $project_key)
279
                    ->whereIn('_id', $d['pt'])
280
                    ->get([ 'name' ]);
281
                foreach ($ps as $val)
282
                {
283
                    $parents[$val['_id']->__toString()] = $val['name'];
284
                }
285
286
                foreach ($d['pt'] as $pid)
287
                {
288 View Code Duplication
                    if (isset($parents[$pid])) {
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...
289
                        $path[] = [ 'id' => $pid, 'name' => $parents[$pid] ];
290
                    }
291
                }
292
            }
293
            $path[] = [ 'id' => $directory, 'name' => $d['name'] ];
294
        }
295
296
        return response()->json([ 'ecode' => 0, 'data' => parent::arrange($documents), 'options' => [ 'path' => $path ] ]);
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...
297
    }
298
299
    /**
300
     * Store a newly created resource in storage.
301
     *
302
     * @param  \Illuminate\Http\Request $request
303
     * @param  string                   $project_key
304
     * @return \Illuminate\Http\Response
305
     */
306 View Code Duplication
    public function createFolder(Request $request, $project_key)
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...
307
    {
308
        $insValues = [];
309
310
        $parent = $request->input('parent');
311
        if (!isset($parent)) {
312
            throw new \UnexpectedValueException('the parent directory can not be empty.', -11905);
313
        }
314
        $insValues['parent'] = $parent;
315
316
        if ($parent !== '0') {
317
            $isExists = DB::collection('document_' . $project_key)
318
                ->where('_id', $parent)
319
                ->where('d', 1)
320
                ->where('del_flag', '<>', 1)
321
                ->exists();
322
            if (!$isExists) {
323
                throw new \UnexpectedValueException('the parent directory does not exist.', -11906);
324
            }
325
        }
326
327
        $name =  $request->input('name');
328
        if (!isset($name) || !$name) {
329
            throw new \UnexpectedValueException('the name can not be empty.', -11900);
330
        }
331
        $insValues['name'] = $name;
332
333
        $isExists = DB::collection('document_' . $project_key)
334
            ->where('parent', $parent)
335
            ->where('name', $name)
336
            ->where('d', 1)
337
            ->where('del_flag', '<>', 1)
338
            ->exists();
339
        if ($isExists) {
340
            throw new \UnexpectedValueException('the name cannot be repeated.', -11901);
341
        }
342
343
        $insValues['pt'] = $this->getParentTree($project_key, $parent);
344
        $insValues['d'] = 1;
345
        $insValues['creator'] = [ 'id' => $this->user->id, 'name' => $this->user->first_name, 'email' => $this->user->email ];
346
        $insValues['created_at'] = time();
347
348
        $id = DB::collection('document_' . $project_key)->insertGetId($insValues);
349
350
        $document = DB::collection('document_' . $project_key)->where('_id', $id)->first();
351
        return response()->json(['ecode' => 0, 'data' => parent::arrange($document)]);
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 createFolder()). 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...
352
    }
353
354
    /**
355
     * get parent treee.
356
     *
357
     * @param  string $project_key
358
     * @param  string $directory
359
     * @return array
360
     */
361 View Code Duplication
    public function getParentTree($project_key, $directory)
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...
362
    {
363
        $pt = [];
0 ignored issues
show
Unused Code introduced by
$pt is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
364
        if ($directory === '0') {
365
            $pt = [ '0' ];
366
        }
367
        else
368
        {
369
            $d = DB::collection('document_' . $project_key)
370
                ->where('_id', $directory)
371
                ->first();
372
            $pt = array_merge($d['pt'], [ $directory ]);
373
        }
374
        return $pt;
375
    }
376
377
    /**
378
     * Update the specified resource in storage.
379
     *
380
     * @param  \Illuminate\Http\Request $request
381
     * @param  string                   $project_key
382
     * @param  string                   $id
383
     * @return \Illuminate\Http\Response
384
     */
385
    public function update(Request $request, $project_key, $id)
386
    {
387
        $name = $request->input('name');
388
        if (!isset($name) || !$name) {
389
            throw new \UnexpectedValueException('the name can not be empty.', -11900);
390
        }
391
392
        $old_document = DB::collection('document_' . $project_key)
393
            ->where('_id', $id)
394
            ->where('del_flag', '<>', 1)
395
            ->first();
396
        if (!$old_document) {
397
            throw new \UnexpectedValueException('the object does not exist.', -11902);
398
        }
399
400 View Code Duplication
        if (isset($old_document['d']) && $old_document['d'] === 1) {
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...
401
            if (!$this->isPermissionAllowed($project_key, 'manage_project')) {
402
                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...
403
            }
404
        }
405
        else
406
        {
407
            if (!$this->isPermissionAllowed($project_key, 'manage_project') && $old_document['uploader']['id'] !== $this->user->id) {
408
                return response()->json(['ecode' => -10002, 'emsg' => 'permission denied.']);
409
            }
410
        }
411
412 View Code Duplication
        if ($old_document['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...
413
            $query = DB::collection('document_' . $project_key)
414
                ->where('parent', $old_document['parent'])
415
                ->where('name', $name)
416
                ->where('del_flag', '<>', 1);
417
418
            if (isset($old_document['d']) && $old_document['d'] === 1) {
419
                $query->where('d', 1);
420
            }
421
            else
422
            {
423
                $query->where('d', '<>', 1);
424
            }
425
426
            $isExists = $query->exists();
427
428
            if ($isExists) {
429
                throw new \UnexpectedValueException('the name cannot be repeated.', -11901);
430
            }
431
        }
432
433
        DB::collection('document_' . $project_key)->where('_id', $id)->update([ 'name' => $name ]);
434
        $new_document = DB::collection('document_' . $project_key)->where('_id', $id)->first();
435
436
        return response()->json(['ecode' => 0, 'data' => parent::arrange($new_document)]);
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...
437
    }
438
439
    /**
440
     * move the document.
441
     *
442
     * @param  \Illuminate\Http\Request $request
443
     * @param  string                   $project_key
444
     * @return \Illuminate\Http\Response
445
     */
446
    public function move(Request $request, $project_key)
447
    {
448
        $id =  $request->input('id');
449
        if (!isset($id) || !$id) {
450
            throw new \UnexpectedValueException('the move object can not be empty.', -11911);
451
        }
452
453
        $dest_path =  $request->input('dest_path');
454
        if (!isset($dest_path)) {
455
            throw new \UnexpectedValueException('the dest directory can not be empty.', -11912);
456
        }
457
458
        $document = DB::collection('document_' . $project_key)
459
            ->where('_id', $id)
460
            ->where('del_flag', '<>', 1)
461
            ->first();
462
        if (!$document) {
463
            throw new \UnexpectedValueException('the move object does not exist.', -11913);
464
        }
465
466 View Code Duplication
        if (isset($document['d']) && $document['d'] === 1) {
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...
467
            if (!$this->isPermissionAllowed($project_key, 'manage_project')) {
468
                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...
469
            }
470
        }
471
        else
472
        {
473
            if (!$this->isPermissionAllowed($project_key, 'manage_project') && $document['uploader']['id'] !== $this->user->id) {
474
                return response()->json(['ecode' => -10002, 'emsg' => 'permission denied.']);
475
            }
476
        }
477
478
        $dest_directory = [];
479
        if ($dest_path !== '0') {
480
            $dest_directory = DB::collection('document_' . $project_key)
481
                ->where('_id', $dest_path)
482
                ->where('d', 1)
483
                ->where('del_flag', '<>', 1)
484
                ->first();
485
            if (!$dest_directory) {
486
                throw new \UnexpectedValueException('the dest directory does not exist.', -11914);
487
            }
488
        }
489
490
        $isExists = DB::collection('document_' . $project_key)
491
            ->where('parent', $dest_path)
492
            ->where('name', $document['name'])
493
            ->where('d', isset($document['d']) && $document['d'] === 1 ? '=' : '<>', 1)
494
            ->where('del_flag', '<>', 1)
495
            ->exists();
496
        if ($isExists) {
497
            throw new \UnexpectedValueException('the name cannot be repeated.', -11901);
498
        }
499
500
        $updValues = [];
501
        $updValues['parent'] = $dest_path;
502
        $updValues['pt'] = array_merge(isset($dest_directory['pt']) ? $dest_directory['pt'] : [], [$dest_path]);
503
        DB::collection('document_' . $project_key)->where('_id', $id)->update($updValues);
504
505 View Code Duplication
        if (isset($document['d']) && $document['d'] === 1) {
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...
506
            $subs = DB::collection('document_' . $project_key)
507
                ->where('pt', $id)
508
                ->where('del_flag', '<>', 1)
509
                ->get();
510
            foreach ($subs as $sub)
511
             {
512
                $pt = isset($sub['pt']) ? $sub['pt'] : [];
513
                $pind = array_search($id, $pt);
514
                if ($pind !== false) {
515
                    $tail = array_slice($pt, $pind);
516
                    $pt = array_merge($updValues['pt'], $tail);
517
                    DB::collection('document_' . $project_key)->where('_id', $sub['_id']->__toString())->update(['pt' => $pt]);
518
                }
519
            }
520
        }
521
522
        $document = DB::collection('document_' . $project_key)->where('_id', $id)->first();
523
        return response()->json(['ecode' => 0, 'data' => parent::arrange($document)]);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (arrange() instead of move()). 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...
524
    }
525
526
    /**
527
     * Remove the specified resource from storage.
528
     *
529
     * @param  string $project_key
530
     * @param  string $id
531
     * @return \Illuminate\Http\Response
532
     */
533
    public function destroy($project_key, $id)
534
    {
535
        $document = DB::collection('document_' . $project_key)
536
            ->where('_id', $id)
537
            ->where('del_flag', '<>', 1)
538
            ->first();
539
        if (!$document) {
540
            throw new \UnexpectedValueException('the object does not exist.', -11902);
541
        }
542
543 View Code Duplication
        if (isset($document['d']) && $document['d'] === 1) {
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...
544
            if (!$this->isPermissionAllowed($project_key, 'manage_project')) {
545
                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...
546
            }
547
        }
548
        else
549
        {
550
            if (!$this->isPermissionAllowed($project_key, 'manage_project') && $document['uploader']['id'] !== $this->user->id) {
551
                return response()->json(['ecode' => -10002, 'emsg' => 'permission denied.']);
552
            }
553
        }
554
555
        DB::collection('document_' . $project_key)->where('_id', $id)->update([ 'del_flag' => 1 ]);
556
557 View Code Duplication
        if (isset($document['d']) && $document['d'] === 1) {
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...
558
            DB::collection('document_' . $project_key)->whereRaw([ 'pt' => $id ])->update([ 'del_flag' => 1 ]);
559
        }
560
561
        return response()->json(['ecode' => 0, 'data' => [ 'id' => $id ]]);
562
    }
563
564
    /**
565
     * Upload file.
566
     *
567
     * @param  \Illuminate\Http\Request $request
568
     * @param  String                   $project_key
569
     * @param  String                   $directory
570
     * @return \Illuminate\Http\Response
571
     */
572
    public function upload(Request $request, $project_key, $directory)
573
    {
574
        set_time_limit(0);
575
576
        if (!is_writable(config('filesystems.disks.local.root', '/tmp'))) {
577
            throw new \UnexpectedValueException('the user has not the writable permission to the directory.', -15103);
578
        }
579
580
        if ($directory !== '0') {
581
            $isExists = DB::collection('document_' . $project_key)
582
                ->where('_id', $directory)
583
                ->where('d', 1)
584
                ->where('del_flag', '<>', 1)
585
                ->exists();
586
            if (!$isExists) {
587
                throw new \UnexpectedValueException('the parent directory does not exist.', -11905);
588
            }
589
        }
590
591
        $fields = array_keys($_FILES);
592
        $field = array_pop($fields);
593 View Code Duplication
        if (empty($_FILES) || $_FILES[$field]['error'] > 0) {
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...
594
            throw new \UnexpectedValueException('upload file errors.', -11903);
595
        }
596
597
        $basename = md5(microtime() . $_FILES[$field]['name']);
598
        $sub_save_path = config('filesystems.disks.local.root', '/tmp') . '/' . substr($basename, 0, 2) . '/';
599
        if (!is_dir($sub_save_path)) {
600
            @mkdir($sub_save_path);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
601
        }
602
603
        $filename = $sub_save_path . $basename;
604
        move_uploaded_file($_FILES[$field]['tmp_name'], $filename);
605
606
        $data = [];
607
608
        $thumbnail_size = 190;
609 View Code Duplication
        if (in_array($_FILES[$field]['type'], [ 'image/jpeg', 'image/jpg', 'image/png', 'image/gif' ])) {
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...
610
            $size = getimagesize($filename);
611
            $width = $size[0]; $height = $size[1];
612
            $scale = $width < $height ? $height : $width;
613
            $thumbnails_width = floor($thumbnail_size * $width / $scale);
614
            $thumbnails_height = floor($thumbnail_size * $height / $scale);
615
            $thumbnails_filename = $filename . '_thumbnails';
616
            if ($scale <= $thumbnail_size) {
617
                @copy($filename, $thumbnails_filename);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
618
            }
619
            else if ($_FILES[$field]['type'] == 'image/jpeg' || $_FILES[$field]['type'] == 'image/jpg') {
620
                $src_image = imagecreatefromjpeg($filename);
621
                $dst_image = imagecreatetruecolor($thumbnails_width, $thumbnails_height);
622
                imagecopyresized($dst_image, $src_image, 0, 0, 0, 0, $thumbnails_width, $thumbnails_height, $width, $height);
623
                imagejpeg($dst_image, $thumbnails_filename);
624
            }
625
            else if ($_FILES[$field]['type'] == 'image/png') {
626
                $src_image = imagecreatefrompng($filename);
627
                $dst_image = imagecreatetruecolor($thumbnails_width, $thumbnails_height);
628
                imagecopyresized($dst_image, $src_image, 0, 0, 0, 0, $thumbnails_width, $thumbnails_height, $width, $height);
629
                imagepng($dst_image, $thumbnails_filename);
630
            }
631
            else if ($_FILES[$field]['type'] == 'image/gif') {
632
                $src_image = imagecreatefromgif($filename);
633
                $dst_image = imagecreatetruecolor($thumbnails_width, $thumbnails_height);
634
                imagecopyresized($dst_image, $src_image, 0, 0, 0, 0, $thumbnails_width, $thumbnails_height, $width, $height);
635
                imagegif($dst_image, $thumbnails_filename);
636
            }
637
            else
638
            {
639
                @copy($filename, $thumbnails_filename);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
640
            }
641
            $data['thumbnails_index'] = $basename . '_thumbnails';
642
            // move the thumbnails
643
            @rename($thumbnails_filename, $sub_save_path . $data['thumbnails_index']);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
644
        }
645
646
        $fname = $_FILES[$field]['name'];
647
        $extname = '';
648
        $segments = explode('.', $fname);
649
        if (count($segments) > 1) {
650
            $extname = '.' . array_pop($segments);
651
            $fname = implode('.', $segments);
652
        }
653
        $i = 1;
654
        while(true)
655
        {
656
            $isExists = DB::collection('document_' . $project_key)
657
                ->where('parent', $directory)
658
                ->where('name', $fname . ($i < 2 ? '' : ('(' . $i . ')')) . $extname)
659
                ->where('d', '<>', 1)
660
                ->where('del_flag', '<>', 1)
661
                ->exists();
662
            if (!$isExists) {
663
                break;
664
            }
665
            $i++;
666
        }
667
        $data['name'] = $fname . ($i < 2 ? '' : ('(' . $i . ')')) . $extname;
668
669
        $data['pt']      = $this->getParentTree($project_key, $directory);
670
        $data['parent']  = $directory;
671
        $data['size']    = $_FILES[$field]['size'];
672
        $data['type']    = $_FILES[$field]['type'];
673
        $data['index']   = $basename;
674
675
        $data['uploader'] = [ 'id' => $this->user->id, 'name' => $this->user->first_name, 'email' => $this->user->email ];
676
        $data['uploaded_at'] = time();
677
678
        $id = DB::collection('document_' . $project_key)->insertGetId($data);
679
        $document = DB::collection('document_' . $project_key)->where('_id', $id)->first();
680
681
        return response()->json(['ecode' => 0, 'data' => parent::arrange($document)]);
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 upload()). 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...
682
    }
683
684
    /**
685
     * Download Thumbnails file.
686
     *
687
     * @param  \Illuminate\Http\Request $request
688
     * @param  String                   $project_key
689
     * @param  String                   $id
690
     * @return \Illuminate\Http\Response
691
     */
692
    public function downloadThumbnails(Request $request, $project_key, $id)
693
    {
694
        set_time_limit(0);
695
696
        $document = DB::collection('document_' . $project_key)
697
            ->where('_id', $id)
698
            ->first();
699
        if (!$document) {
700
            throw new \UnexpectedValueException('the object does not exist.', -11902);
701
        }
702
703
        $filepath = config('filesystems.disks.local.root', '/tmp') . '/' . substr($document['index'], 0, 2);
704
        $filename = $filepath . '/' . $document['thumbnails_index'];
705
        if (!file_exists($filename)) {
706
            throw new \UnexpectedValueException('file does not exist.', -11904);
707
        }
708
709
        File::download($filename, $document['name']);
710
    }
711
712
    /**
713
     * Download file or directory.
714
     *
715
     * @param  \Illuminate\Http\Request $request
716
     * @param  String                   $project_key
717
     * @param  String                   $id
718
     * @return \Illuminate\Http\Response
719
     */
720
    public function download(Request $request, $project_key, $id)
721
    {
722
        set_time_limit(0);
723
724
        $document = DB::collection('document_' . $project_key)
725
            ->where('_id', $id)
726
            ->first();
727
        if (!$document) {
728
            throw new \UnexpectedValueException('the object does not exist.', -11902);
729
        }
730
731
        if (isset($document['d']) && $document['d'] === 1) {
732
            $this->downloadFolder($project_key, $document['name'], $id);
733
        }
734
        else
735
        {
736
            $this->downloadFile($document['name'], $document['index']);
737
        }
738
    }
739
740
    /**
741
     * Download file.
742
     *
743
     * @param  String $name
744
     * @param  String $directory
745
     * @return \Illuminate\Http\Response
746
     */
747
    public function downloadFolder($project_key, $name, $directory)
748
    {
749
        setlocale(LC_ALL, 'zh_CN.UTF-8'); 
750
751
        $basepath = '/tmp/' . md5($this->user->id . microtime());
752
        @mkdir($basepath);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
753
754
        $this->contructFolder($project_key, $basepath . '/' . $name, $directory);
755
756
        $filename = $basepath . '/' . $name . '.zip';
757
758
        Zipper::make($filename)->folder($name)->add($basepath . '/' . $name);
759
        Zipper::close();
760
761
        File::download($filename, $name . '.zip');
762
763
        exec('rm -rf ' . $basepath);
764
    }
765
766
    /**
767
     * contruct file folder.
768
     *
769
     * @param  String $fullpath
770
     * @param  String $id
771
     * @return void
772
     */
773
    public function contructFolder($project_key, $fullpath, $id)
774
    {
775
        @mkdir($fullpath);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
776
777
        $documents = DB::collection('document_' . $project_key)
778
            ->where('parent', $id)
779
            ->where('del_flag', '<>', 1)
780
            ->get();
781
        foreach ($documents as $doc)
782
        {
783
            if (isset($doc['d']) && $doc['d'] === 1) {
784
                $this->contructFolder($project_key, $fullpath . '/' . $doc['name'], $doc['_id']->__toString());
785
            }
786
            else
787
            {
788
                $filepath = config('filesystems.disks.local.root', '/tmp') . '/' . substr($doc['index'], 0, 2);
789
                $filename = $filepath . '/' . $doc['index'];
790
                if (file_exists($filename)) {
791
                    @copy($filename, $fullpath . '/' . $doc['name']);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
792
                }
793
            }
794
        }
795
    }
796
797
    /**
798
     * Download file.
799
     *
800
     * @param  String $name
801
     * @param  String $index
802
     * @return \Illuminate\Http\Response
803
     */
804 View Code Duplication
    public function downloadFile($name, $index)
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...
805
    {
806
        $filepath = config('filesystems.disks.local.root', '/tmp') . '/' . substr($index, 0, 2);
807
        $filename = $filepath . '/' . $index;
808
        if (!file_exists($filename)) {
809
            throw new \UnexpectedValueException('file does not exist.', -11904);
810
        }
811
812
        File::download($filename, $name);
813
    }
814
815
    /**
816
     * favorite action.
817
     *
818
     * @param  string $project_key
819
     * @param  string $id
820
     * @return \Illuminate\Http\Response
821
     */
822 View Code Duplication
    public function favorite(Request $request, $project_key, $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...
823
    {
824
        $document = DB::collection('document_' . $project_key)
825
            ->where('_id', $id)
826
            ->where('del_flag', '<>', 1)
827
            ->first();
828
        if (!$document) {
829
            throw new \UnexpectedValueException('the object does not exist.', -11902);
830
        }
831
832
        DocumentFavorites::where('did', $id)->where('user.id', $this->user->id)->delete();
833
834
        $cur_user = [ 'id' => $this->user->id, 'name' => $this->user->first_name, 'email' => $this->user->email ];
835
836
        $flag = $request->input('flag');
837
        if (isset($flag) && $flag) {
838
            DocumentFavorites::create([ 'project_key' => $project_key, 'did' => $id, 'user' => $cur_user ]);
839
        }
840
841
        return response()->json(['ecode' => 0, 'data' => ['id' => $id, 'user' => $cur_user, 'favorited' => $flag]]);
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...
842
    }
843
}
844