OpenDocumentController::__invoke()   B
last analyzed

Complexity

Conditions 9
Paths 33

Size

Total Lines 81
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 54
CRAP Score 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 48
c 1
b 0
f 0
nc 33
nop 1
dl 0
loc 81
ccs 54
cts 54
cp 1
crap 9
rs 7.5789

How to fix   Long Method   

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 LinkRestrictedAccess\Http\Controllers;
4
5
use Carbon\Carbon;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Facades\Cookie;
8
use Illuminate\Support\Facades\Redirect;
9
use Illuminate\Support\Facades\Response;
10
use Illuminate\Validation\Rule;
11
use Illuminate\Validation\ValidationException;
12
use LinkRestrictedAccess\Models\RestrictedLink;
13
use LinkRestrictedAccess\Models\RestrictedLinkOpenAction;
14
15
class OpenDocumentController extends Controller
16
{
17 3
    public function __invoke(Request $request)
18
    {
19
        /** @var RestrictedLink $restrictedLink */
20 3
        $restrictedLink = \LinkRestrictedAccess\RestrictedAccess::restrictedLinkModel()::query()
21 3
            ->where('uuid', $request->route('uuid'))
22 3
            ->firstOrFail();
23
24 3
        $request->validate([
25 3
            'pin' => [
26 3
                'nullable',
27 3
                Rule::requiredIf($restrictedLink->checkPin),
0 ignored issues
show
Bug introduced by
The property checkPin does not seem to exist on LinkRestrictedAccess\Models\RestrictedLink. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
28 3
                'string',
29 3
                'max:100',
30 3
            ],
31 3
            'name' => [
32 3
                'nullable',
33 3
                Rule::requiredIf($restrictedLink->checkName),
0 ignored issues
show
Bug introduced by
The property checkName does not seem to exist on LinkRestrictedAccess\Models\RestrictedLink. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
34 3
                'string',
35 3
                'max:255',
36 3
            ],
37 3
            'email' => [
38 3
                'nullable',
39 3
                Rule::requiredIf($restrictedLink->checkEmail),
0 ignored issues
show
Bug introduced by
The property checkEmail does not seem to exist on LinkRestrictedAccess\Models\RestrictedLink. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
40 3
                'email',
41 3
                'max:255',
42 3
            ],
43 3
        ]);
44
45
        if (
46 3
            $restrictedLink->checkPin &&
47
            (
48 3
                !$restrictedLink->pin ||
0 ignored issues
show
Bug introduced by
The property pin does not seem to exist on LinkRestrictedAccess\Models\RestrictedLink. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
49 3
                $restrictedLink->pin != $request->input('pin')
50
            )
51
52
        ) {
53 1
            throw ValidationException::withMessages([
54 1
                'pin' => __('Incorrect pin'),
55 1
            ]);
56
        }
57
58
        /** @var RestrictedLinkOpenAction $openAction */
59 3
        $openAction = $restrictedLink->openActions()->make([
60 3
            'viewed_at' => Carbon::now(),
61 3
        ])->fillBrowserFingerPrint($request);
62
63
64 3
        if ($user = $request->user()) {
65 1
            $openAction->viewer()->associate($user);
66
        }
67
68 3
        if ($restrictedLink->checkPin) {
69 3
            $openAction->verification_result->setAttribute('pin_checked', true);
70
        }
71
72 3
        if ($restrictedLink->checkName) {
73 3
            $openAction->verification_result->setAttribute('name', $request->input('name'));
74
        }
75
76 3
        if ($restrictedLink->checkEmail) {
77 3
            $openAction->verification_result->setAttribute('email', $request->input('email'));
78
        }
79
80 3
        $openAction->save();
81
82 3
        $message = __('Access allowed.');
83
84 3
        $cookie = Cookie::make($restrictedLink->cookieName(), $openAction->uuid, 60 * 24 /* 24 hours */);
85
86 3
        if ($request->expectsJson()) {
87 2
            return Response::json([
88 2
                'message' => $message,
89 2
                'data'    => [
90 2
                    'success' => true,
91 2
                ],
92 2
            ])->withCookie($cookie);
93
        }
94
95 1
        return Redirect::back()
96 1
            ->with(['message' => $message])
97 1
            ->withCookie($cookie);
98
    }
99
}
100