EntryController::store()   C
last analyzed

Complexity

Conditions 11
Paths 55

Size

Total Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 62
rs 6.6824
c 0
b 0
f 0
cc 11
nc 55
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 Kalizi\LaravelSpyhole\Http\Controllers;
4
5
use Illuminate\Http\JsonResponse;
6
use Illuminate\Routing\Controller;
7
use Illuminate\Support\Facades\Auth;
8
use Illuminate\Support\Str;
9
use Kalizi\LaravelSpyhole\Http\Requests\StoreEntryRequest;
10
use Kalizi\LaravelSpyhole\Models\SessionRecording;
11
use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
12
13
class EntryController extends Controller
14
{
15
    public function store(StoreEntryRequest $request): JsonResponse
16
    {
17
        $recordingId = null;
18
        if ($request->has('recording')) {
19
            $recordingId = (int)decrypt($request->get('recording'));
20
21
            if (SessionRecording::whereId($recordingId)->count() === 0) {
22
                throw new NotAcceptableHttpException();
23
            }
24
        }
25
26
        if (config('laravel-spyhole.track_request_session_id')) {
27
            $sessionId = $request->session()->getId();
0 ignored issues
show
Bug introduced by
The method getId cannot be called on $request->session() (of type callable).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
28
        } else {
29
            if (session()->has('spyhole_session_id')) {
30
                $sessionId = session()->get('spyhole_session_id');
31
            } else {
32
                do {
33
                    $sessionId = Str::uuid()->toString();
34
                } while (
35
                    SessionRecording::whereSessionId($sessionId)->count() > 0 &&
36
                    $sessionId !== $request->session()->getId()
0 ignored issues
show
Bug introduced by
The method getId cannot be called on $request->session() (of type callable).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
37
                );
38
                session()->put('spyhole_session_id', $sessionId);
39
            }
40
        }
41
42
        $userId = null;
43
        if (config('laravel-spyhole.record_user_id')) {
44
            $user = Auth::user();
45
            $userId = $user ? $user->getAuthIdentifier() : null;
46
        }
47
48
        if ($recordingId === null) {
49
            $recording = new SessionRecording();
50
            $recording->session_id = $sessionId;
51
            $recording->user_id = $userId;
52
            $recording->path = $request->get('path');
53
            $recording->recordings = $request->get('frames');
54
        } else {
55
            $recording = SessionRecording::wherePath($request->get('path'))
56
                ->whereId($recordingId)
57
                ->first();
58
59
            if ($recording === null) {
60
                throw new NotAcceptableHttpException();
61
            }
62
63
            // Merge frames from the same session
64
            $recording->recordings = array_merge(
65
                $recording->recordings,
66
                $request->get('frames')
67
            );
68
        }
69
70
        $recording->save();
71
72
        return response()->json([
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...
73
            'success' => true,
74
            'recording' => encrypt($recording->id),
75
        ]);
76
    }
77
}
78