Issues (61)

src/Http/Controllers/UploadController.php (15 issues)

1
<?php
2
3
namespace Itstructure\MFU\Http\Controllers;
4
5
use Throwable;
6
use Illuminate\Http\Request;
7
use Symfony\Component\HttpKernel\Exception\HttpException;
8
use Itstructure\MFU\Facades\{Uploader, Previewer};
9
use Itstructure\MFU\Models\Mediafile;
10
11
/**
12
 * Class UploadController
13
 * @package Itstructure\MFU\Http\Controllers
14
 */
15
class UploadController extends BaseController
16
{
17
    /**
18
     * @param Request $request
19
     * @return \Illuminate\Http\JsonResponse
20
     * @throws HttpException
21
     */
22
    public function upload(Request $request)
23
    {
24
        try {
25
            $data = $request->post('data');
26
            $file = $request->file('file');
27
28
            if (!Uploader::upload($data, $file)) {
0 ignored issues
show
The method upload() does not exist on Itstructure\MFU\Facades\Uploader. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
            if (!Uploader::/** @scrutinizer ignore-call */ upload($data, $file)) {
Loading history...
29
                return response()->json([
30
                    'success' => false,
31
                    'errors' => Uploader::hasErrors()
0 ignored issues
show
The method hasErrors() does not exist on Itstructure\MFU\Facades\Uploader. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
                    'errors' => Uploader::/** @scrutinizer ignore-call */ hasErrors()
Loading history...
32
                        ? Uploader::getErrors()->getMessages()
0 ignored issues
show
The method getErrors() does not exist on Itstructure\MFU\Facades\Uploader. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

32
                        ? Uploader::/** @scrutinizer ignore-call */ getErrors()->getMessages()
Loading history...
33
                        : []
34
                ]);
35
            }
36
            return response()->json([
37
                'success' => true,
38
                'id' => Uploader::getId()
0 ignored issues
show
The method getId() does not exist on Itstructure\MFU\Facades\Uploader. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

38
                'id' => Uploader::/** @scrutinizer ignore-call */ getId()
Loading history...
39
            ]);
40
41
        } catch (Throwable $exception) {
42
            return abort(400, $exception->getMessage());
0 ignored issues
show
Are you sure the usage of abort(400, $exception->getMessage()) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug Best Practice introduced by
The expression return abort(400, $exception->getMessage()) returns the type void which is incompatible with the documented return type Illuminate\Http\JsonResponse.
Loading history...
43
        }
44
    }
45
46
    /**
47
     * @param Request $request
48
     * @return \Illuminate\Http\JsonResponse
49
     * @throws HttpException
50
     */
51
    public function update(Request $request)
52
    {
53
        try {
54
            $id = $request->post('id');
55
            $data = $request->post('data');
56
            $file = $request->hasFile('file') ? $request->file('file') : null;
57
58
            if (!Uploader::update($id, $data, $file)) {
0 ignored issues
show
The method update() does not exist on Itstructure\MFU\Facades\Uploader. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
            if (!Uploader::/** @scrutinizer ignore-call */ update($id, $data, $file)) {
Loading history...
59
                return response()->json([
60
                    'success' => false,
61
                    'errors' => Uploader::hasErrors()
62
                        ? Uploader::getErrors()->getMessages()
63
                        : []
64
                ]);
65
            }
66
            return response()->json([
67
                'success' => true
68
            ]);
69
70
        } catch (Throwable $exception) {
71
            return abort(400, $exception->getMessage());
0 ignored issues
show
Are you sure the usage of abort(400, $exception->getMessage()) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug Best Practice introduced by
The expression return abort(400, $exception->getMessage()) returns the type void which is incompatible with the documented return type Illuminate\Http\JsonResponse.
Loading history...
72
        }
73
    }
74
75
    /**
76
     * @param Request $request
77
     * @return \Illuminate\Http\JsonResponse
78
     * @throws HttpException
79
     */
80
    public function delete(Request $request)
81
    {
82
        try {
83
            return response()->json([
84
                'success' => Uploader::delete($request->post('id'))
0 ignored issues
show
The method delete() does not exist on Itstructure\MFU\Facades\Uploader. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

84
                'success' => Uploader::/** @scrutinizer ignore-call */ delete($request->post('id'))
Loading history...
85
            ]);
86
87
        } catch (Throwable $exception) {
88
            return abort(400, $exception->getMessage());
0 ignored issues
show
Bug Best Practice introduced by
The expression return abort(400, $exception->getMessage()) returns the type void which is incompatible with the documented return type Illuminate\Http\JsonResponse.
Loading history...
Are you sure the usage of abort(400, $exception->getMessage()) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
89
        }
90
    }
91
92
    /**
93
     * @param Request $request
94
     * @return string
95
     * @throws HttpException
96
     */
97
    public function preview(Request $request)
98
    {
99
        try {
100
            $id = $request->post('id');
101
            $location = $request->post('location') ?? \Itstructure\MFU\Services\Previewer::LOCATION_FILE_INFO;
102
            $mediaFile = Mediafile::find($id);
103
            return Previewer::getPreviewHtml($mediaFile, $location);
0 ignored issues
show
The method getPreviewHtml() does not exist on Itstructure\MFU\Facades\Previewer. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

103
            return Previewer::/** @scrutinizer ignore-call */ getPreviewHtml($mediaFile, $location);
Loading history...
104
105
        } catch (Throwable $exception) {
106
            return abort(400, $exception->getMessage());
0 ignored issues
show
Bug Best Practice introduced by
The expression return abort(400, $exception->getMessage()) returns the type void which is incompatible with the documented return type string.
Loading history...
Are you sure the usage of abort(400, $exception->getMessage()) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
107
        }
108
    }
109
}
110