EntryController   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 8
dl 0
loc 65
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C store() 0 62 11
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