BaseController::sendResponse()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Finder\Http\Controllers;
4
5
use InfyOm\Generator\Utils\ResponseUtil;
6
use Response;
7
use Illuminate\Support\Facades\URL;
8
use Illuminate\Support\Facades\View;
9
use Illuminate\Support\Facades\Auth;
10
use Illuminate\Support\Facades\File;
11
use Illuminate\Support\Facades\Storage;
12
use Illuminate\Support\Str;
13
use Intervention\Image\Constraint;
14
use Intervention\Image\Facades\Image;
15
use League\Flysystem\Util;
16
use Illuminate\Http\Request;
17
18
/**
19
 * @SWG\Swagger(
20
 *   basePath="/api/v1",
21
 * @SWG\Info(
22
 *     title="Laravel Generator APIs",
23
 *     version="1.0.0",
24
 *   )
25
 * )
26
 * This class should be parent class for other API controllers
27
 * Class BaseController
28
 */
29
class BaseController extends Controller
30
{
31
    public function sendResponse($result, $message)
32
    {
33
        return Response::json(ResponseUtil::makeResponse($message, $result));
34
    }
35
36
    public function sendError($error, $code = 404)
37
    {
38
        return Response::json(ResponseUtil::makeError($error), $code);
39
    }
40
41
42
    public function assets(Request $request)
43
    {
44
        try {
45
            $path = dirname(__DIR__, 3).'/publishes/assets/'.Util::normalizeRelativePath(urldecode($request->path));
46
        } catch (\LogicException $e) {
47
            abort(404);
48
        }
49
50
        if (File::exists($path)) {
51
            $mime = '';
0 ignored issues
show
Unused Code introduced by
$mime 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...
52
            if (Str::endsWith($path, '.js')) {
53
                $mime = 'text/javascript';
54
            } elseif (Str::endsWith($path, '.css')) {
55
                $mime = 'text/css';
56
            } else {
57
                $mime = File::mimeType($path);
58
            }
59
            $response = response(File::get($path), 200, ['Content-Type' => $mime]);
60
            $response->setSharedMaxAge(31536000);
0 ignored issues
show
Bug introduced by
The method setSharedMaxAge does only exist in Illuminate\Http\Response, but not in Illuminate\Contracts\Routing\ResponseFactory.

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...
61
            $response->setMaxAge(31536000);
0 ignored issues
show
Bug introduced by
The method setMaxAge does only exist in Illuminate\Http\Response, but not in Illuminate\Contracts\Routing\ResponseFactory.

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...
62
            $response->setExpires(new \DateTime('+1 year'));
0 ignored issues
show
Bug introduced by
The method setExpires does only exist in Illuminate\Http\Response, but not in Illuminate\Contracts\Routing\ResponseFactory.

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...
63
64
            return $response;
65
        }
66
67
        return response('', 404);
68
    }
69
70
}
71